#P1967. [NOIP 2013 提高组] 货车运输

    ID: 913 远端评测题 1000ms 128MiB 尝试: 2 已通过: 0 难度: 7 上传者: 标签>图论贪心2013倍增并查集NOIp 提高组生成树最近公共祖先,LCA

[NOIP 2013 提高组] 货车运输

Description

Country A has nn cities, numbered from 11 to nn, and there are mm bidirectional roads between the cities. Each road has a weight limit for vehicles, referred to as the load limit.

There are qq trucks transporting goods. The drivers want to know, without exceeding any road's weight limit, the maximum weight each truck can carry.

Input Format

The first line contains two integers n,mn,m separated by a single space, indicating that Country A has nn cities and mm roads.

Each of the next mm lines contains three integers x,y,zx, y, z, separated by single spaces, indicating there is a road from city xx to city yy with weight limit zz.
Note: xyx \neq y, and there may be multiple roads between two cities.

The next line contains an integer qq, meaning there are qq trucks that need to transport goods.

Then follow qq lines, each containing two integers x,yx,y separated by a space, indicating a truck needs to transport goods from city xx to city yy, with xyx \neq y guaranteed.

Output Format

Output qq lines, each with an integer, indicating the maximum load for each truck.
If a truck cannot reach the destination, output 1-1.

4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3
3
-1
3

Hint

For 30%30\% of the testdata, 1n<10001 \le n < 1000, 1m<10,0001 \le m < 10,000, 1q<10001\le q< 1000.

For 60%60\% of the testdata, 1n<10001 \le n < 1000, 1m<5×1041 \le m < 5\times 10^4, 1q<10001 \le q< 1000.

For 100%100\% of the testdata, 1n<1041 \le n < 10^4, 1m<5×1041 \le m < 5\times 10^4, 1q<3×1041 \le q< 3\times 10^4 , 0z1050 \le z \le 10^5.

Translated by ChatGPT 5