标签:dijstra
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
注:如果是孤立点的话,直接返回,因为后面有可能会对low数组修改
代码:
/*dijstra*/ #include <stdio.h> #include <string.h> #include <vector> #include <algorithm> using namespace std; #define M 1005 #define INF 1633771873 int map[M][M]; int ss[M], dd[M], low[M], maxx; bool vis[M]; void dijstra(int u){ memset(vis, false, sizeof(vis)); int pos = u, min, i, j; vis[pos] = 1; for(i = 1; i <= maxx; i ++) low[i] = map[pos][i]; for(i = 1; i < M; i ++){ min = INF; for(j = 1; j <= maxx; j ++){ if(!vis[j]&&min > low[j]){ min = low[j]; pos = j; } } vis[pos] = 1; if(min == INF) return; //对孤立点的处理,原因是下面几行会修改low数组的数据 for(j = 1; j <= maxx; j ++){ if(!vis[j]&&map[pos][j]+low[pos] < low[j]){ low[j] = map[pos][j]+low[pos]; } } } } int main(){ int t, s, d, i, j, a, b, c; while(scanf("%d%d%d", &t, &s, &d) == 3){ for(i = 0; i < M; i ++){ for(j = 0; j < M; j ++) map[i][j] = INF; } maxx = 0; for(i = 0; i < t; i ++){ scanf("%d%d%d", &a, &b, &c); if(map[a][b] > c){ map[a][b] = map[b][a] = c; } maxx = max(maxx, max(a, b)); } for(i = 0; i < s; i ++){ scanf("%d", &ss[i]); maxx = max(maxx, ss[i]); } for(i = 0; i < d; i ++){ scanf("%d", &dd[i]); maxx = max(maxx, dd[i]); } int ans = INF; for(i = 0; i < s; i ++){ dijstra(ss[i]); for(j = 0; j < d; j ++){ /*for(int k = 1; k <= maxx; k ++) printf("%d..", low[k]); printf("\n");*/ if(ans > low[dd[j]]) ans = low[dd[j]]; } } printf("%d\n", ans); } return 0; }
标签:dijstra
原文地址:http://blog.csdn.net/shengweisong/article/details/41013751