#P3044. [USACO12FEB] Relocation S

[USACO12FEB] Relocation S

Description

Farmer John is moving and wants to build a new farm so that his daily travel distance is minimized.

The region has NN towns (1N10,0001 \le N \le 10{,}000). There are MM bidirectional roads (1M50,0001 \le M \le 50{,}000) connecting certain pairs of towns, and every town is reachable from every other town by some combination of roads. There are markets in KK towns (1K51 \le K \le 5) that FJ wants to visit every day. Each day, he will leave his new farm, visit all KK market towns in any order he chooses, and then return to his farm.

When choosing a town for his new farm, FJ will only consider the NKN - K towns that do not have markets, since housing prices are lower in those towns.

Please compute the minimum total distance FJ needs to travel in his daily routine if he builds his farm in an optimal town and plans his route to the markets optimally.

Input Format

  • Line 1: Three space-separated integers NN, MM, and KK.
  • Lines 21+K2 \ldots 1+K: Line i+1i+1 contains an integer in the range 1N1 \ldots N identifying the town containing the ii-th market. Each market is in a different town.
  • Lines 2+K1+K+M2+K \ldots 1+K+M: Each line contains three space-separated integers ii, jj (1i,jN1 \le i, j \le N), and LL (1L10001 \le L \le 1000), indicating a bidirectional road of length LL between towns ii and jj.

Output Format

  • Line 1: The minimum distance FJ needs to travel during his daily routine, if he builds his farm in an optimal location.
5 6 3 
1 
2 
3 
1 2 1 
1 5 2 
3 2 3 
3 4 5 
4 2 7 
4 5 10 

12 

Hint

There are 55 towns, with towns 11, 22, and 33 having markets. There are 66 roads. FJ builds his farm in town 55. His daily route is 55-11-22-33-22-11-55, for a total distance of 1212.

Translated by ChatGPT 5