#P8418. [THUPC 2022 决赛] 倍增路径
[THUPC 2022 决赛] 倍增路径
Problem Description
For any DAG and a starting vertex in the graph, the rules of the doubling mini-game are as follows:
-
In the -th round of operation (), the player chooses a non-empty path starting from , such that the sum of edge weights along this path is exactly a multiple of . If it is impossible to choose a path that satisfies the multiple requirement, then the game is considered failed, and the player will receive no score. Otherwise, this round is successful, and the endpoint of is recorded as .
-
After the -th round is successful, the player may choose to end the game or continue to the -th round. If the player chooses to end the game, then the chosen paths are called doubling paths, and the score is calculated.
If the game does not fail, when ending the game, for the selected doubling paths , the score is defined as , where denotes the number of edges in path , and is the given weight from the input. Obviously, no matter how the paths are chosen, at most paths can be selected, so the input only provides .
To set the clearance requirement for each graph, given the DAG and the starting vertex, compute the maximum possible score of this game.
Input Format
The first line contains three positive integers , , separated by spaces, representing the number of vertices, the number of edges, and the index of the starting vertex. It is guaranteed that , , and .
The second line contains positive integers separated by spaces, representing the weights used when calculating the score. It is guaranteed that .
In the next lines, each line contains three positive integers separated by spaces, describing a directed edge from to with weight . It is guaranteed that , , , the graph is connected, and there are no multiple edges.
Output Format
Output exactly one line containing two integers separated by a space.
If at least one path can be selected, then there exists an optimal selection that maximizes the score. Let the simplest fractional form of this optimal score be (if the optimal score is an integer, take ), and output and . Otherwise, if it is impossible to select at least one path no matter what, output -1 -1.
5 5 1
1 11 21 1211
1 2 4
1 3 11
2 5 9
3 4 12
4 5 13
6 1
9 16 1
1 10 100 1000 10000 100000 1000000 10000000
1 2 2
1 3 3
2 3 5
2 5 7
3 4 11
3 5 13
3 6 17
3 7 19
4 7 23
5 6 29
5 8 31
6 7 37
6 8 41
6 9 43
7 9 47
8 9 53
221 3
Hint
[Sample 1 Explanation]
The selected doubling paths are .
[Constraints]
For of the testdata, it is guaranteed that , , , , , the input graph is connected, and there are no multiple edges.
Translated by ChatGPT 5