3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
水了一晚上了,罪过罪过
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #define INF 0x3f3f3f using namespace std; int map[220][220]; int n, m; void floy(){ for(int k = 0; k < n; ++k) for(int i = 0; i < n; ++i) if(map[i][k] != INF){ for(int j = 0; j < n; ++j) map[i][j] = min(map[i][j], map[i][k] + map[k][j]); } } int main (){ while(scanf("%d%d", &n, &m) != EOF){ for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) if(i == j) map[i][j] = 0; else map[i][j] = INF; while(m--){ int u, v, w; scanf("%d%d%d", &u, &v, &w); if(map[u][v] > w) map[u][v] = map[v][u] = w; } int st, ed; scanf("%d%d", &st, &ed); floy(); if(map[st][ed] == INF) printf("-1\n"); else printf("%d\n", map[st][ed]); } return 0; }