从来没写过多源最短路,明天就NOIP初赛了,今天晚上写一下。。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<queue> #include<map> #include<list> #include<cstring> #include<cassert> #include<cmath> using namespace std; long long arr[105][105]; int n; long long q; int main(){ ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ cin >> arr[i][j]; } } for (int k = 1; k <= n; k++){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ arr[i][j] = min(arr[i][j], arr[i][k] + arr[k][j]); } } } cin >> q; for (long long i = 1; i <= q; i++){ int s, e; cin >> s >> e; cout << arr[s][e] << endl; } return 0; } |