D - joisino's travel
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
There are N towns in the State of Atcoder, connected by M bidirectional roads.
The i-th road connects Town Ai and Bi and has a length of Ci.
Joisino is visiting R towns in the state, r1,r2,..,rR (not necessarily in this order).
She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.
If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?
Constraints
- 2≤N≤200
- 1≤M≤N×(N−1)⁄2
- 2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)
- ri≠rj(i≠j)
- 1≤Ai,Bi≤N,Ai≠Bi
- (Ai,Bi)≠(Aj,Bj),(Ai,Bi)≠(Bj,Aj)(i≠j)
- 1≤Ci≤100000
- Every town can be reached from every town by road.
- All input values are integers.
Input
Input is given from Standard Input in the following format:
N M Rr1 … rRA1 B1 C1:AM BM CM
Output
Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.
Sample Input 1
3 3 31 2 31 2 12 3 13 1 4
Sample Output 1
2
For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.
Sample Input 2
3 3 21 32 3 21 3 61 2 2
Sample Output 2
4
The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.
Sample Input 3
4 6 32 3 41 2 42 3 34 3 11 4 14 2 23 1 6
Sample Output 3
Copy
3
//题意,n 个点, m 条边,,R 个需要去的地方,可以从任意一点出发,终于任意一点,但必须走完 R 个点,问最小路径为多少?
//首先,用Floyd求出最短路,然后暴力枚举要走的点的顺序即可 8!也就1千万
1 # include2 # include 3 # include 4 # include 5 # include 6 # include 7 # include 8 # include
//如果R再大点(16左右),还可以状态压缩一下 dp[i][j] 表在 i 点,状态为 j 时的最小路径
1 # include2 # include 3 # include 4 # include 5 # include 6 # include 7 # include 8 # include