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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std;
typedef long long LL; const int inf = 0x3f3f3f3f; const int maxn = 810; int a[maxn];
struct Edge { int from, to, dist; Edge(int u, int v, int d): from(u), to(v), dist(d) {} };
struct Dijkstra { int n, m; vector<Edge> edges; vector<int> G[maxn]; bool done[maxn]; int d[maxn]; int p[maxn];
void init(int n) { this->n = n; for (int i = 0; i <= n; ++i) { G[i].clear(); } edges.clear(); }
void AddEdge(int from, int to, int dist) { edges.push_back(Edge(from, to, dist)); m = int(edges.size()); G[from].push_back(m - 1); }
struct HeapNode { int d, u; HeapNode(int d, int u): d(d), u(u) {} bool operator < (const HeapNode& rhs) const { return d > rhs.d; } };
void dijkstra(int s) { priority_queue<HeapNode> Q; for (int i = 0; i < n; ++i) { d[i] = inf; } d[s] = 0; memset(done, 0, sizeof done); Q.push(HeapNode(0, s)); while (!Q.empty()) { HeapNode x = Q.top(); Q.pop(); int u = x.u; if (done[u]) { continue; } done[u] = true; for (int i = 0; i < G[u].size(); ++i) { Edge& e = edges[G[u][i]]; if (d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; p[e.to] = G[u][i]; Q.push(HeapNode(d[e.to], e.to)); } } } } } dij;
int main() { freopen("butter.in", "r", stdin); freopen("butter.out", "w", stdout); int N, P, C; scanf("%d%d%d", &N, &P, &C); for (int i = 0; i < N; ++i) { scanf("%d", &a[i]); } dij.init(P + 1); for (int i = 0; i < C; ++i) { int u, v, len; scanf("%d%d%d", &u, &v, &len); dij.AddEdge(u, v, len); dij.AddEdge(v, u, len); } int ans = inf; for (int i = 1; i <= P; ++i) { dij.dijkstra(i); int res = 0; for (int j = 0; j < N; ++j) { res += dij.d[a[j]]; if (res > inf) { res = inf; } } if (res < ans) { ans = res; } } printf("%d\n", ans); return 0; }
|