标签:
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<queue> # include<stack> # include<cstdio> # include<cstring> # include<algorithm> # define MAXN 1000 + 10 # define MAXM 5000 + 10 # define INF 0x3f3f3f3f using namespace std; int t, s, d, cnt; int vis[MAXN]; int head[MAXN]; int dist[MAXN]; struct node { int from, to, val, next; }p[MAXM]; void add(int u, int v, int w) { p[cnt].from = u; p[cnt].to = v; p[cnt].val = w; p[cnt].next = head[u]; head[u] = cnt++; } void get() { int a, b, c; while(t--) { scanf("%d%d%d",&a,&b,&c); add(a, b, c); add(b, a, c); } } void init() { cnt = 0; memset(head, -1, sizeof(head)); memset(vis, 0, sizeof(head)); } void spfa(int px) { vis[px] = 1; dist[px] = 0; queue<int>Q; Q.push(px); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = p[i].next) { int v = p[i].to; if(dist[v] > dist[u] + p[i].val) { dist[v] = dist[u] + p[i].val; if(!vis[v]) { vis[v] = 1; Q.push(v); } } } } } int main() { while(scanf("%d%d%d",&t,&s,&d)!=EOF) { init(); get(); int r, x, time; while(s--) { scanf("%d",&x); add(0, x, 0); add(x, 0, 0);//比较纠结的地方在这里,将x点与源点的距离赋值为0 } for(int i = 1; i <= 1000; i++) { dist[i] = INF; } spfa(0); time = INF; while(d--) { scanf("%d",&r); if(time > dist[r]) time = dist[r]; } printf("%d\n",time); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/b_mess/article/details/47804613