-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestRoutes2.cpp
More file actions
57 lines (48 loc) · 1.16 KB
/
ShortestRoutes2.cpp
File metadata and controls
57 lines (48 loc) · 1.16 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
//Shortest Routes II - https://cses.fi/problemset/task/1672
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector<vector<pair<int, int>>> g(n);
vector<vector<ll>> d(n, vector<ll>(n, INF));
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
d[u][v] = min(d[u][v], (ll) w);
d[v][u] = min(d[v][u], (ll) w);
}
for (int i = 0; i < n; i++) {
d[i][i] = 0;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (d[i][k] < INF && d[k][j] < INF) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
for (int i = 0; i < q; i++) {
int u, v;
cin >> u >> v;
u--, v--;
ll ans = d[u][v];
cout << (ans < INF ? ans : -1) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}