#K50592. Minimum Distance to Collect Points

    ID: 28898 Type: Default 1000ms 256MiB

Minimum Distance to Collect Points

Minimum Distance to Collect Points

You are given a road network of n cities and m bidirectional roads. Each road connects two cities and has a given length. In addition, you are provided a sequence of k cities that must be visited in a specific order.

Your task is to find the minimum total distance required to travel along the shortest paths between consecutive cities in the sequence. Use Dijkstra's algorithm to compute the shortest path between any pair of cities. For each test case, sum the shortest distances connecting each pair of consecutive cities in the sequence.

Note: All input is read from standard input and all output is written to standard output.

inputFormat

The input begins with a single integer T representing the number of test cases. Each test case is described as follows:

  • The first line contains two integers n and m — the number of cities and roads.
  • The next m lines each contain three integers u, v, and w representing a bidirectional road between cities u and v with length w.
  • The following line contains an integer k, the number of cities in the travel sequence.
  • The next line contains k integers denoting the sequence of cities to visit in order.

outputFormat

For each test case, output a single line with one integer — the minimum total distance required to traverse the sequence of cities.

## sample
3
5 6
1 2 2
1 3 5
2 3 1
2 4 4
3 5 3
4 5 1
3
1 3 5
3 3
1 2 3
2 3 2
1 3 4
2
1 3
4 4
1 2 1
2 3 2
3 4 3
1 4 10
3
1 3 4
6

4 6

</p>