标签:
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> #include <queue> using namespace std; #define INF 0xfffffff #define N 1002 bool vis[N]; int n, m, dist[N], G[N][N]; void Init() { memset(vis, false, sizeof(vis)); for(int i = 0; i <= n; i++) { dist[i] = INF; for(int j = 0; j <= n; j++) G[i][j] = G[j][i] = INF; } } int dij(int s, int e) { dist[s] = 0; for(int i = 0; i <= n; i++) { int index = 0, Min = INF; for(int j = 0; j <= n; j++) { if(!vis[j] && dist[j] < Min) Min = dist[j], index = j; } vis[index] = true; for(int j = 0; j <= n; j++) { if(!vis[j] && dist[j] > dist[index] + G[index][j]) dist[j] = dist[index] + G[index][j]; } } return dist[e]; } int main() { int a, b, c; while(cin >> n >> m, n+m) { Init(); for(int i = 0; i < m; i++) { cin >> a >> b >> c; G[a][b] = G[a][b] < c ? G[a][b] : c; G[b][a] = G[a][b]; } cout << dij(1, n) << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4658149.html