标签:
#include<cstring>
#include<iostream>
using namespace std;
const int N=105, INF=0x3f3f3f3f;
int shortEdge[N], w[N][N],vis[N],n,m;
void dijkstra(int src){
for(int i=1; i<=n; ++i)
shortEdge[i] = INF;
shortEdge[src] = 0;
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; ++i){
int u=-1;
for(int j=1; j<=n; ++j)if(!vis[j]){
if(u==-1 || shortEdge[j]<shortEdge[u]) u=j;
}
vis[u] = 1;
//调整数组
for(int j=1; j<=n; ++j)if(!vis[j]){
int tmp = shortEdge[u] + w[u][j];
if(tmp<shortEdge[j]) shortEdge[j] = tmp;
}
}
}
int main(){
int a,b,c;
while(cin >> n >> m){
if(n==0 && m == 0) break;
for(int i=1; i<=n; ++i){
w[i][i] = INF;
for(int j=i+1; j<=n; ++j)
w[i][j] = w[j][i] = INF;
}
for(int i=0; i<m; ++i){
cin >> a >> b >> c;
w[a][b] = w[b][a] = c;
}
dijkstra(1);
cout << shortEdge[n] << endl;
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/2016zhanggang/p/5638914.html