#P1690. 贪婪的 Copy

贪婪的 Copy

Description

Copy heard from Lu Niu that many treasures are buried in a divine land called yz, so Copy came to this land, which is divided into nn regions. Lu Niu told Copy that there are in total PP treasures, each placed in region PiP_i (1Pin1 \le P_i \le n). Copy also learned the distance between every pair of regions. Now Copy starts from region 11, wants to collect all treasures, and leave from region nn. Copy is lazy, so he asks you to find a suitable route that minimizes the total distance he needs to travel.

Input Format

  • The first line contains a positive integer nn (1n1001 \le n \le 100).
  • The next nn lines each contain nn integers; the number in row ii, column jj denotes the distance from region ii to region jj. The numbers are separated by spaces, and each distance is at most 10001000. Note that the distance iji \to j is not necessarily equal to jij \to i.
  • The next line contains an integer PP (0P100 \le P \le 10).
  • The next line contains PP integers separated by spaces, denoting the indices of the regions that contain treasures.

Output Format

Output a single integer: the minimum total distance for Copy to collect all treasures and then reach region nn. The testdata guarantees that the answer is at most 23112^{31} - 1.

2
0 4
5 0
2
1 2

4

3
0 2 6
1 0 4
7 10 0
1
2

6

Hint

  • For 30%30\% of the testdata, 1n151 \le n \le 15; the rest are as stated.
  • For 100%100\% of the testdata, all constraints are as stated.

Translated by ChatGPT 5