-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloyd 多源最短路径.cpp
44 lines (39 loc) · 859 Bytes
/
Floyd 多源最短路径.cpp
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
38
39
40
41
42
43
44
#include <iostream>
#include <stdio.h>
#include <cstring>
#define N 1005
#define INF (int)1e8
using namespace std;
int mp[N][N];
int dp[N][N];
int main() {
int n,m,t; cin>>n>>m>>t;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
mp[i][j] = INF;
}
}
while(m--) {
int x,y,c; scanf("%d%d%d",&x,&y,&c);
mp[x][y] = c;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
dp[i][j] = mp[i][j];
}
}
for(int k=1;k<=n;k++) {
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);
}
}
}
while(t--) {
int from,to; scanf("%d%d",&from,&to);
int ans = dp[from][to];
if(ans == INF) ans = -1;
printf("%d\n",ans);
}
return 0;
}