标签:最短路
6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10
9
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 1010; const int inf = 0x3f3f3f3f; int dist[N]; int head[N]; int want[N]; int tot, n, m; typedef pair<int, int> PLL; struct node { int weight; int next; int to; }edge[N * N]; void addedge(int from, int to, int weight) { edge[tot].weight = weight; edge[tot].to = to; edge[tot].next = head[from]; head[from] = tot++; } void dijkstra(int v0) { memset (dist, inf, sizeof(dist)); dist[v0] = 0; priority_queue < PLL, vector<PLL>, greater<PLL> > qu; while (!qu.empty()) { qu.pop(); } dist[v0] = 0; qu.push(make_pair(dist[v0], v0)); while (!qu.empty()) { PLL tmp = qu.top(); qu.pop(); int u = tmp.second; int d = tmp.first; for (int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; if (dist[v] > d + edge[i].weight) { dist[v] = d + edge[i].weight; qu.push(make_pair(dist[v], v)); } } } } int main() { int t, s, d, u, v, w; while (~scanf("%d%d%d", &t, &s, &d)) { memset (head, -1, sizeof(head)); tot = 0; for (int i = 0; i < t; ++i) { scanf("%d%d%d", &u, &v, &w); addedge(u, v, w); addedge(v, u, w); } for (int i = 0; i < s; ++i) { scanf("%d", &u); addedge(0, u, 0); addedge(u, 0, 0); } for (int i = 0; i < d; ++i) { scanf("%d", &want[i]); } dijkstra(0); int ans = inf; for (int i = 0; i < d; ++i) { ans = min(ans, dist[want[i]]); } printf("%d\n", ans); } return 0; }
标签:最短路
原文地址:http://blog.csdn.net/guard_mine/article/details/41346359