aboutsummaryrefslogtreecommitdiff
path: root/04_graph_algorithms/shortest_routes_ii_1672.cpp
diff options
context:
space:
mode:
Diffstat (limited to '04_graph_algorithms/shortest_routes_ii_1672.cpp')
-rw-r--r--04_graph_algorithms/shortest_routes_ii_1672.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/04_graph_algorithms/shortest_routes_ii_1672.cpp b/04_graph_algorithms/shortest_routes_ii_1672.cpp
new file mode 100644
index 0000000..d49cb71
--- /dev/null
+++ b/04_graph_algorithms/shortest_routes_ii_1672.cpp
@@ -0,0 +1,30 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5constexpr size_t inf{999999999999999ULL};
6
7int main() {
8 size_t n, m, q;
9 std::cin >> n >> m >> q;
10 std::vector<std::vector<size_t>> d(n, std::vector(n, inf));
11 for (size_t i = 0; i < n; i++) d[i][i] = 0;
12 for (size_t i = 0; i < m; i++) {
13 size_t x, y, z;
14 std::cin >> x >> y >> z;
15 d[x-1][y-1] = d[y-1][x-1] = std::min(d[x-1][y-1], z);
16 }
17
18 for (size_t k = 0; k < n; k++)
19 for (size_t i = 0; i < n; i++)
20 for (size_t j = 0; j < n; j++)
21 d[j][i] = d[i][j] =
22 std::min(d[i][j], d[i][k] + d[k][j]);
23
24 for (size_t i = 0; i < q; i++) {
25 size_t x, y;
26 std::cin >> x >> y;
27 long long int dd = d[x-1][y-1];
28 std::cout << (dd == inf ? -1LL : dd) << "\n";
29 }
30}

Generated with cgit - Back to sebastiano.tronto.net