#abc280f. F - Pay or Receive

F - Pay or Receive

Score : 500500 points

问题描述

设有 NN 个编号为 1,,N1,\ldots,N 的城镇和 MM 条编号为 1,,M1,\ldots,M 的道路。

ii 条道路连接城镇 AiA_iBiB_i。当你使用一条道路时,你的 得分 如下变化:

  • 当你通过道路 ii 从城镇 AiA_i 移动到城镇 BiB_i 时,你的得分增加 CiC_i
  • 当你通过道路 ii 从城镇 BiB_i 移动到城镇 AiA_i 时,你的得分减少 CiC_i

你的得分可能变为负数。

回答以下 QQ 个问题。

  • 如果你从城镇 XiX_i 出发,初始得分为 00,求当你到达城镇 YiY_i 时可以获得的最大得分。
    • 如果无法从城镇 XiX_i 到达城镇 YiY_i,输出 nan
    • 如果在到达城镇 YiY_i 时可以得到任意大的得分,输出 inf

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN towns numbered 1,,N1,\ldots,N and MM roads numbered 1,,M1,\ldots,M.

Road ii connects towns AiA_i and BiB_i. When you use a road, your score changes as follows:

  • when you move from town AiA_i to town BiB_i using road ii, your score increases by CiC_i; when you move from town BiB_i to town AiA_i using road ii, your score decreases by CiC_i.

Your score may become negative.

Answer the following QQ questions.

  • If you start traveling from town XiX_i with initial score 00, find the maximum possible score when you are at town YiY_i.
    Here, if you cannot get from town XiX_i to town YiY_i, print nan instead; if you can have as large a score as you want when you are at town YiY_i, print inf instead.

Constraints

  • 2N1052\leq N \leq 10^5
  • 0M1050\leq M \leq 10^5
  • 1Q1051\leq Q \leq 10^5
  • 1Ai,Bi,Xi,YiN1\leq A_i,B_i,X_i,Y_i \leq N
  • 0Ci1090\leq C_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM QQ

A1A_1 B1B_1 C1C_1

\vdots

AMA_M BMB_M CMC_M

X1X_1 Y1Y_1

\vdots

XQX_Q YQY_Q

Output

Print QQ lines as specified in the Problem Statement.
The ii-th line should contain the answer to the ii-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 55 to move from town 55 to town 33, you can have a score 2-2 when you are at town 33.
Since you cannot make the score larger, the answer is 2-2.

For the second question, you can have as large a score as you want when you are at town 22 if you travel as follows: repeatedly "use road 22 to move from town 11 to town 22 and then use road 11 to move from town 22 to town 11" as many times as you want, and finally use road 22 to move from town 11 to town 22.

For the third question, you cannot get from town 33 to town 11.

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