1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f; int n, m, r; int ar[20]; int mp[205][205]; int dp[1<<16][20]; int x, y, z;
void floyd() { for(int k = 1; k <= n; ++k) { for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n; ++j) mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]); } } }
int main() { scanf("%d%d%d", &n, &m, &r); for(int i = 1; i <= r; ++i) scanf("%d", &ar[i]);
memset(mp, 0x3f, sizeof(mp)); memset(dp, 0x3f, sizeof(dp));
for(int i = 1; i <= n; ++i) mp[i][i] = 0;
while(m--) { scanf("%d%d%d", &x, &y, &z); mp[x][y] = mp[y][x] = z; }
floyd();
for(int i = 1; i <= r; ++i) dp[1<<(i-1)][i] = 0;
for(int s = 0; s < (1<<r) - 1; ++s) { for(int i = 1; i <= r; ++i) { if(!(s >> (i - 1) & 1)) continue; for(int j = 1; j <= r; ++j) { if(s >> (j - 1) & 1) continue; if(mp[ar[i]][ar[j]] != inf) { dp[s|(1<<(j-1))][j] = min(dp[s][i] + mp[ar[i]][ar[j]], dp[s|(1<<(j-1))][j]); } } } }
int ans = 0x3f3f3f3f;
for(int i = 1; i <= r; ++i) { ans = min(ans, dp[(1<<r) - 1][i]); }
printf("%d\n", ans); return 0; }
|