哇,flody大法好,居然只有几行代码?!
不过好像我体会不出它的精妙出来。
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 205;
int n,m;
int mp[maxn][maxn];
int main() {
freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m) != EOF) {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if( i == j) mp[i][j] = mp[j][i] = 0;
else mp[i][j] = 1e9;
int x,y,z;
for(int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
mp[x][y] = min(z, mp[x][y]);
}
int s,t;
scanf("%d%d",&s,&t);
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
mp[i][j] = min(mp[i][j], mp[i][k]+mp[k][j]);
if(mp[s][t] == 1e9) cout << "-1" << endl;
else cout << mp[s][t] << endl;
}
}