C語言源碼: [cpp] #include<stdio.h> #include<limits.h> #include<string.h> #define maxsize 110 int s[maxsize][maxsize]; int main() { int n,m,i,j,k,time; scanf("%d %d",&n,&m); while(n!=0||m!=0) { for(i=0;i<n;i++) for(j=0;j<n;j++) s[i][j]=INT_MAX; for(k=0;k<m;k++) { scanf("%d %d %d",&i,&j,&time); s[i-1][j-1]=time; s[j-1][i-1]=time; } for(k=0;k<n;k++) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(s[i][k]==INT_MAX||s[k][j]==INT_MAX) continue; else if(s[i][k]+s[k][j]<s[i][j]) s[i][j]=s[i][k]+s[k][j]; } } } printf("%d\n",s[0][n-1]); scanf("%d %d",&n,&m); } }