[cce_cpp] #include <iostream> #include <algorithm> using namespace std; #ifndef ONLINE_JUDGE //最开始想用来练习spfa+邻接表,老是wa,改用floyd + 邻接矩阵,还是wa,最后无奈看了discuss, //才发现忘记判断起点与终点相同的情况... #include <fstream> ifstream fin("test.txt"); #define cin fin #endif const int INF = 99999999; int graph[220][220]; int main() { ios::sync_with_stdio(false); int n,m,i,a,b,x,s,t,k,j; while(cin >> n >> m) { for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) if(i==j) graph[i][j]=0; else graph[i][j] = INF; } for(i = 0; i < m; ++i) { cin >> a >> b >> x; graph[a][b] = graph[b][a] = x < graph[a][b] ? x : graph[a][b]; } cin >> s >> t; for(k = 0; k < n; ++k) for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) if(graph[i][j] > graph[i][k] + graph[k][j]) graph[i][j] = graph[i][k] + graph[k][j]; if(graph[s][t]==INF) cout << -1 << endl; else cout << graph[s][t] << endl; } return 0; } [/cce_cpp]