atcoder#ABC261H. [ABC261Ex] Game on Graph
[ABC261Ex] Game on Graph
Score : points
Problem Statement
We have a directed graph with vertices and edges. Edge is directed from Vertex to and has a weight of .
Initially, there is a piece on Vertex . Takahashi and Aoki will play a game where they alternate turns moving the piece as follows:
- If there is no edge that goes from the vertex on which the piece is placed, end the game.
- If there are edges that go from the vertex on which the piece is placed, choose one of those edges and move the piece along that edge.
Takahashi goes first. Takahashi tries to minimize the total weight of the edges traversed by the piece, and Aoki tries to maximize it. More formally, their objectives are as follows. Takahashi gives the first priority to ending the game in a finite number of moves. If this is possible, he tries to minimize the total weight of the edges traversed by the piece. Aoki gives the first priority to preventing the game from ending in a finite number of moves. If this is impossible, he tries to maximize the total weight of the edges traversed by the piece. (If the piece traverses the same edge multiple times, the weight is added that number of times.)
Determine whether the game ends in a finite number of moves when both players play optimally. If it ends, find the total weight of the edges traversed by the piece.
Constraints
- There is no multi-edges. That is, for .
- There is no self-loops. That is, .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If the game does not end in a finite number of moves when both players play optimally, print INFINITY
.
If the game ends in a finite number of moves, print the total weight of the edges traversed by the piece.
7 6 1
1 2 1
1 3 10
2 4 100
2 5 102
3 6 20
3 7 30
40
First, Takahashi will move the piece to Vertex . Next, Aoki will move the piece to Vertex , and the game will end. The total weight of the edges traversed by the piece will be .
3 6 3
1 2 1
2 1 2
2 3 3
3 2 4
3 1 5
1 3 6
INFINITY
The game will not end in a finite number of moves.
4 4 1
1 2 1
2 3 1
3 1 1
2 4 1
5
The piece will go .