标签:des blog io os ar java for sp strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7400 Accepted Submission(s): 2426
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <map> #include <vector> using namespace std; const int N = 1005; const int inf = 0X7000000; int n,m,s,p,q,t,w; int d[N][N],fa[N],dist[N],ans; int ww[N]; void init(){ for(int i = 0; i <= n; i++){ for(int j = 0; j <= n; j++) d[i][j] = inf; fa[i] = i; dist[i] = inf; } } int find(int x){ if(x == fa[x]) return x; return fa[x] = find(fa[x]); } void Union(int x, int y){ x = find(x); y = find(y); if(x!=y) fa[y] = x; } void dij(int start){ for(int i = 0; i <= n; i++){ dist[i] = d[start][i]; } dist[start] = 0; for(int i = 1; i <= n; i++){ int mn = inf; int pos = start; for(int j = 1; j <=n; j++){ if(fa[j] != start && dist[j] < mn){ pos = j; mn = dist[j]; } } Union(start,pos); for(int j = 1; j <= n; j++){ if(fa[j] != start && (dist[pos]+d[pos][j]) < dist[j]) dist[j] = dist[pos]+d[pos][j]; } } ans = dist[s]; } int main(){ while(~scanf("%d %d %d",&n, &m,&s)){ ans = inf; init(); while(m--){ scanf("%d %d %d",&p, &q, &t); if(t < d[p][q]) d[p][q] = t; //好像巴士不是双向的。。 } scanf("%d",&w); for(int i = 0; i < w; i++){ scanf("%d", &m); d[0][m] = 0; } dij(0); if(ans == inf) ans = -1; printf("%d\n", ans); } return 0; }
标签:des blog io os ar java for sp strong
原文地址:http://www.cnblogs.com/brokesb/p/4067224.html