#abc280f. F - Pay or Receive
F - Pay or Receive
Score : points
问题描述
设有 个编号为 的城镇和 条编号为 的道路。
第 条道路连接城镇 和 。当你使用一条道路时,你的 得分 如下变化:
- 当你通过道路 从城镇 移动到城镇 时,你的得分增加 ;
- 当你通过道路 从城镇 移动到城镇 时,你的得分减少 。
你的得分可能变为负数。
回答以下 个问题。
- 如果你从城镇 出发,初始得分为 ,求当你到达城镇 时可以获得的最大得分。
- 如果无法从城镇 到达城镇 ,输出
nan
; - 如果在到达城镇 时可以得到任意大的得分,输出
inf
。
- 如果无法从城镇 到达城镇 ,输出
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are towns numbered and roads numbered .
Road connects towns and . When you use a road, your score changes as follows:
- when you move from town to town using road , your score increases by ; when you move from town to town using road , your score decreases by .
Your score may become negative.
Answer the following questions.
- If you start traveling from town with initial score , find the maximum possible score when you are at town .
Here, if you cannot get from town to town , printnan
instead; if you can have as large a score as you want when you are at town , printinf
instead.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines as specified in the Problem Statement.
The -th line should contain the answer to the -th question.
Sample Input 1
5 5 3
1 2 1
1 2 2
3 4 1
4 5 1
3 5 2
5 3
1 2
3 1
Sample Output 1
-2
inf
nan
For the first question, if you use road to move from town to town , you can have a score when you are at town .
Since you cannot make the score larger, the answer is .
For the second question, you can have as large a score as you want when you are at town if you travel as follows: repeatedly "use road to move from town to town and then use road to move from town to town " as many times as you want, and finally use road to move from town to town .
For the third question, you cannot get from town to town .
Sample Input 2
2 1 1
1 1 1
1 1
Sample Output 2
inf
The endpoints of a road may be the same, and so may the endpoints given in a question.
Sample Input 3
9 7 5
3 1 4
1 5 9
2 6 5
3 5 8
9 7 9
3 2 3
8 4 6
2 6
4 3
3 8
3 2
7 9
Sample Output 3
inf
nan
nan
inf
-9
update @ 2024/3/10 11:46:37