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
| #include <bits/stdc++.h> #define fir first #define sec second #define mp make_pair #define MAX_N 100000 using namespace std; typedef long long lnt; typedef pair<lnt,int> pli; typedef priority_queue<pli> pri_que; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, k; lnt f[32][MAX_N+5]; bool mrk[32][MAX_N+5]; vector <int> G[MAX_N+5]; vector <lnt> E[MAX_N+5]; pri_que que; void insert(int u, int v, lnt c) {G[u].push_back(v), E[u].push_back(c);} void addedge(int u, int v, lnt c) {insert(u, v, c), insert(v, u, c);} int main() { read(n), read(k), read(m), memset(f, 0x3f, sizeof f); for (int i = 0, p; i < k; i++) read(p), f[1<<i][p] = 0; for (int i = 1, u, v, c; i <= m; i++) read(u), read(v), read(c), addedge(u, v, c); for (int s = 1; s < (1<<k); s++) { for (int i = 1; i <= n; i++) for (int t = (s-1)&s; t; t = (t-1)&s) f[s][i] = min(f[s][i], f[t][i]+f[s^t][i]); for (int i = 1; i <= n; i++) que.push(mp(-f[s][i], i)); while (!que.empty()) { int u = que.top().sec; que.pop(); if (mrk[s][u]) continue; mrk[s][u] = true; for (int i = 0, v; i < (int)G[u].size(); i++) if (f[s][v = G[u][i]] > f[s][u]+E[u][i]) f[s][v] = f[s][u]+E[u][i], que.push(mp(-f[s][v], v)); } } lnt mi = 1LL<<62; for (int i = 1; i <= n; i++) mi = min(mi, f[(1<<k)-1][i]); return printf("%lld\n", mi), 0; }
|