#abc061d. [abc061_d]Score Attack
[abc061_d]Score Attack
Problem Statement
There is a directed graph with vertices and edges. The -th edge points from vertex to vertex , and has a weight . We will play the following single-player game using this graph and a piece.
Initially, the piece is placed at vertex , and the score of the player is set to . The player can move the piece as follows:
- When the piece is placed at vertex , move the piece along the -th edge to vertex . After this move, the score of the player is increased by .
The player can end the game only when the piece is placed at vertex . The given graph guarantees that it is possible to traverse from vertex to vertex .
When the player acts optimally to maximize the score at the end of the game, what will the score be? If it is possible to increase the score indefinitely, print inf
.
Constraints
- or
- is an integer.
- In the given graph, there exists a path from vertex to vertex .
Input
Input is given from Standard Input in the following format:
Output
Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf
.
Sample Input 1
3 3
1 2 4
2 3 3
1 3 5
Sample Output 1
7
There are two ways to move the piece to vertex :
- vertex → vertex → vertex : score
- vertex → vertex : score
Thus, the maximum possible score at the end of the game is .
Sample Input 2
2 2
1 2 1
2 1 1
Sample Output 2
inf
It is possible to increase the score indefinitely by alternating between vertex and .
Sample Input 3
6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000
Sample Output 3
-5000000000