-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1672.cpp
More file actions
32 lines (30 loc) · 723 Bytes
/
Copy path1672.cpp
File metadata and controls
32 lines (30 loc) · 723 Bytes
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m, q;
ll d[505][505];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
fill(&d[0][0], &d[0][0] + sizeof(d) / sizeof(ll), 1e18);
for (int i = 1; i <= n; ++i) {
d[i][i] = 0;
}
for (int i = 1, u, v, c; i <= m; ++i) {
cin >> u >> v >> c;
d[u][v] = d[v][u] = min(d[u][v], (ll)c);
}
for (int k = 1; k <= n; ++k) {
for (int u = 1; u <= n; ++u) {
for (int v = 1; v <= n; ++v) {
d[u][v] = min(d[u][v], d[u][k] + d[k][v]);
}
}
}
while (q--) {
int u, v; cin >> u >> v;
if (d[u][v] == 1e18) d[u][v] = -1;
cout << d[u][v] << "\n";
}
return 0;
}