链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2544
题目:
输入保证至少存在1条商店到赛场的路线。
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
32
//#include "stdafx.h" #include<cstring> #include<cstdio> #include<queue> using namespace std; const int N=105; const int INF=9999999; int n,m,d[N][N]; void read_graph() { for(int i=1;i<=n;++i) { d[i][i]=INF; for(int j=i+1;j<=n;++j) d[i][j]=d[j][i]=INF; } int a,b,c; for(int e=1;e<=m;e++) { scanf("%d%d%d",&a,&b,&c); d[a][b]=d[b][a]=c; } } void Floyd(int src) { for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(d[i][k]<INF&&d[k][j]<INF) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } int main() { int a,b,c; while(~scanf("%d%d",&n,&m)&&(n+m)) { read_graph(); Floyd(1); printf("%d\n",d[1][n]); } return 0; }