#P5304. [GXOI/GZOI2019] 旅行者

[GXOI/GZOI2019] 旅行者

Description

Country J has nn cities, connected by mm one-way roads. The length of each road is known.

One day, Rainbow, who lives in Country J, invites Vani to visit. However, as an experienced traveler, Vani is only interested in kk cities in Country J that have a long history and unique natural scenery.
To improve the travel experience, Vani wants to know the minimum value among the “pairwise shortest paths” between the cities he is interested in (that is, among all pairs of cities he is interested in, the shortest-path distance of the closest pair).

You may have already guessed the story — Vani will be busy traveling elsewhere these days, so please help him solve this problem.

Input Format

Each test point contains multiple test cases. The first line is an integer TT, the number of test cases. Note that the test cases are independent of each other.

For each test case, the first line contains three positive integers n,m,kn, m, k, representing nn cities in Country J (numbered from 1n1 \sim n), mm roads, and the number kk of cities that Vani is interested in.

The next mm lines each contain 33 positive integers x,y,zx, y, z, indicating that there is a one-way road from city xx to city yy with length zz. Note that xx and yy may be equal, and the same pair (x,y)(x, y) may appear multiple times.

The next line contains kk positive integers, the indices of the cities that Vani is interested in.

Output Format

The output should contain TT lines. For each test case, output one integer: the minimum value among the pairwise shortest-path distances between the kk cities.

2
6 7 3
1 5 3
2 3 5
1 4 3
5 3 2
4 6 5
4 3 7
5 6 4
1 3 6
7 7 4
5 3 10
6 2 7
1 2 6
5 4 2
4 3 4
1 7 3
7 2 4
1 2 5 3
5
6

Hint

Sample Explanation

For the first test case, the shortest path from 11 to 33 is 55; from 11 to 66 is 77; 33 and 66 cannot reach each other. So the closest pair is 1,31, 3, and the minimum distance is 55.

For the second test case, the shortest path from 11 to 22 is 66; from 55 to 33 is 66; all other pairs of points cannot reach each other. So the closest pairs are 1,21, 2 and 5,35, 3, and the minimum distance is 66.

Constraints

2kn2 \le k \le n, 1x,yn1 \le x, y \le n, 1z2×1091 \le z \le 2 \times 10^9, T5T \leq 5.

Test Point ID Scale of nn Scale of mm Notes
11 1,000\le 1,000 5,000\le 5,000 None
22
33 100,000\le 100,000 500,000\le 500,000 The graph is guaranteed to be a directed acyclic graph
44
55
66 None
77
88
99
1010

Admin note on 2024-12-18: There may be self-loops in test point 5

Translated by ChatGPT 5