Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 37213 Accepted Submission(s): 13725
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> #define INF 9999999 #define min(a, b) a < b ? a : b; int map[210][210]; void floyd(int n){ int i, j, k; for(k = 0; k < n; ++k){ for(i = 0; i < n; ++i){ for(j = 0; j < n; ++j){ map[i][j] = min(map[i][j], map[i][k] + map[k][j]); } } } } int main(){ int n, m; while(~scanf("%d%d", &n, &m)){ int u, v, w, i, j; 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 = 0; i < m; ++i){ scanf("%d%d%d", &u, &v, &w); if(map[u][v] > w) map[u][v] = map[v][u] = w; } floyd(n); scanf("%d%d", &u, &v); if(map[u][v] != INF) printf("%d\n", map[u][v]); else printf("-1\n"); } return 0; }