标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
int w[maxn][maxn], d[maxn];
bool vis[maxn];
int n, m;
void dijkstra(){
memset(vis, false, sizeof(bool)*(n+1));
for (int i=1; i<=n; ++i)
d[i] = (i==1 ? 0 : inf);
for (int i=1; i<=n; ++i){
int x=0, m=inf;
for (int y=1; y<=n; ++y)
if (!vis[y] && d[y]<=m)
m = d[x=y];
vis[x] = true;
for (int y=1; y<=n; ++y)
d[y] = min(d[y], d[x]+w[x][y]);
}
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
while (~scanf("%d%d", &n, &m) && n+m){
int x, y, c;
for (x=1; x<=n; ++x)
for (y=1; y<=n; ++y)
w[x][y] = (x==y ? 0 : inf);
for (int i=1; i<=m; ++i){
scanf("%d%d%d", &x, &y, &c);
w[x][y] = w[y][x] = c;
}
dijkstra();
printf("%d\n", d[n]);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/silenceneo/article/details/51369967