B - 畅通工程续 -- floyd 算法
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=76#problem/B
#include <stdio.h> #include <string.h> #include <math.h> #define INF 1<<20 int map[201][201]; void floyd(int n) { int i,j,k; for(k = 0;k<=n;k++) { for(i = 0;i<=n;i++) { for(j = 0;j<=n;j++) { if(map[i][j]>map[i][k] + map[k][j]) { map[i][j] = map[i][k] + map[k][j]; } } } } } int n,m,s,t; int main() { int i,j; int u,v,w; while(scanf("%d%d",&n,&m)!=EOF) { for(i = 0;i<=n;i++) { for(j = 0;j<=n;j++) { if(i == j) map[i][j] = 0; else map[i][j] = INF; } } for(i = 1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); if(map[v][u]>w) { map[u][v] = map[v][u] = w; } } floyd(n); scanf("%d%d",&s,&t); if(map[s][t]>=INF) printf("-1\n"); else printf("%d\n",map[s][t]); } return 0; }