标签:
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
#include <stdio.h> #include <string.h> #include <algorithm> #define inf 0x3f3f3f3f using namespace std; const int N =205; int map[N][N]; int m,n; int main(){ int i,j,k; int s,t; while(~scanf("%d %d",&n,&m)){ 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++){ int x,y,z; scanf("%d %d %d",&x,&y,&z); map[x][y]=min(map[x][y],z); map[y][x]=min(map[y][x],z); } scanf("%d %d",&s,&t); 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[j][k]) map[i][j]=map[i][k]+map[k][j]; if(map[s][t]==inf) printf("-1\n"); else printf("%d\n",map[s][t]); } return 0; }
标签:
原文地址:http://blog.csdn.net/qq_27717967/article/details/51348099