Edges and Divisors
Time limit1sMemory limit2048 MB
Given a weighted DAG and weights a_1 to a_(n-1), choose a chain of paths whose lengths are multiples of i+1 in round i, maximizing the weighted average score.
- Level
Hard10 of 10
- Topics
- Graph, Dynamic programming, Number theory
- Solved
- No attempts yet
Problem
Time limit: 1000 ms. Memory limit: 2048 MB.
We play a game on a directed acyclic graph . We start at vertex . The rules are as follows:
- The game consists of rounds numbered from .
- In the -th round, the player selects a path that starts at and contains at least one edge, such that the sum of the weights of its edges is an exact multiple of . If no such path exists, the player fails and scores no points. Otherwise, the round ends successfully, and the endpoint of is recorded as .
- After a successful round, the player can either end the game or continue with the next round. If the player ends the game, the selected paths are called doubling paths, and the score is calculated.
If the player has not failed, then when the game ends, the score for the selected doubling paths is , where is the number of edges in path , and is the weight given in the input. Since the graph is acyclic, at most paths can be selected, so the input provides only the weights .
Given the graph and the starting vertex, compute the maximum achievable score.
Input
The first line contains three integers , , and : the number of vertices, the number of edges, and the index of the starting vertex (; ; ).
The second line contains integers , the weights used to calculate the score ().
Each of the next lines contains three integers , , and , describing a directed edge from to with weight (; ; ). The graph is acyclic, connected when edges are treated as undirected, and has no multiple edges.
Output
Output a single line containing two integers separated by a space.
If at least one path can be selected, an optimal selection scheme exists, and the optimal score can be written as where and are coprime integers and . In this case, output and . Otherwise, if no path can be selected, output -1~-1.
Hint
The selected doubling paths are and .