标签:hdu1874
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
#include <stdio.h> #include <string.h> #define maxn 202 const int maxint = 100000000; int n, m; int dist[maxn], map[maxn][maxn]; bool vis[maxn]; int getNextNode() { int i, tmp = maxint, u = -1; for(i = 0; i < n; ++i){ if(dist[i] != -1 && !vis[i] && dist[i] < tmp){ tmp = dist[i]; u = i; } } return u; } void Dijkstra(int start, int end) { memset(dist, -1, sizeof(dist)); memset(vis, 0, sizeof(vis)); int i, tmp, u; dist[start] = 0; vis[start] = true; for(i = 0; i < n; ++i){ if(map[start][i] != -1) dist[i] = map[start][i]; } u = getNextNode(); while(u != -1){ for(i = 0; i < n; ++i){ if(map[u][i] != -1 && !vis[i]){ tmp = dist[u] + map[u][i]; if(dist[i] == -1 || dist[i] > tmp) dist[i] = tmp; } } vis[u] = true; u = getNextNode(); } } int main() { int i, a, b, x, s, t; while(scanf("%d%d", &n, &m) != EOF){ memset(map, -1, sizeof(map)); for(i = 0; i < m; ++i){ scanf("%d%d%d", &a, &b, &x); if(map[a][b] != -1 && x > map[a][b]) continue; map[a][b] = map[b][a] = x; } scanf("%d%d", &s, &t); Dijkstra(s, t); printf("%d\n", dist[t]); } return 0; }
HDU1874 畅通工程续 【Dijkstra】,布布扣,bubuko.com
标签:hdu1874
原文地址:http://blog.csdn.net/chang_mu/article/details/38172311