题目链接:
http://acm.uestc.edu.cn/#/problem/show/30
题意:
题解:
直接floyd
代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){ 10 ll x=0,f=1;char ch=getchar(); 11 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 ////////////////////////////////////////////////////////////////////////// 16 const int maxn = 100+10; 17 18 int d[maxn][maxn]; 19 20 int main(){ 21 int n,m; 22 while(scanf("%d%d",&n,&m),n+m){ 23 for(int i=0; i<=n; i++) 24 for(int j=0; j<=n; j++) 25 d[i][j] = INF; 26 for(int i=0; i<m; i++){ 27 int u,v,w; scanf("%d%d%d",&u,&v,&w); 28 d[u][v] = d[v][u] = w; 29 } 30 31 for(int k=1; k<=n; k++) 32 for(int i=1; i<=n; i++) 33 for(int j=1; j<=n; j++) 34 d[i][j] = min(d[i][j],d[i][k]+d[k][j]); 35 36 cout << d[1][n] << endl; 37 } 38 39 return 0; 40 }