This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ruthen71/rcpl
#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path" #include <bits/stdc++.h> #include "graph/dijkstra.hpp" #include "graph/read_graph.hpp" int main() { int N, M, s, t; std::cin >> N >> M >> s >> t; auto G = read_graph<long long>(N, M, true, true, 0); std::vector<int> ss = {s}; const long long INF = 1LL << 60; auto [d, p, r] = dijkstra(G, ss, INF); if (d[t] == INF) { std::cout << -1 << '\n'; return 0; } std::vector<int> ans; int c = t; while (t != -1) { ans.push_back(t); t = p[t]; } std::reverse(ans.begin(), ans.end()); std::cout << d[c] << ' ' << ans.size() - 1 << '\n'; for (int i = 0; i < ans.size() - 1; i++) std::cout << ans[i] << ' ' << ans[i + 1] << '\n'; return 0; }
#line 1 "verify/lc_graph/lc_shortest_path_dijkstra.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/shortest_path" #include <bits/stdc++.h> #line 2 "graph/dijkstra.hpp" #line 2 "graph/graph_template.hpp" #line 4 "graph/graph_template.hpp" template <class T> struct Edge { int from, to; T cost; int id; Edge() = default; Edge(int from, int to, T cost = 1, int id = -1) : from(from), to(to), cost(cost), id(id) {} friend std::ostream &operator<<(std::ostream &os, const Edge<T> &e) { // output format: "{ id : from -> to, cost }" return os << "{ " << e.id << " : " << e.from << " -> " << e.to << ", " << e.cost << " }"; } }; template <class T> using Edges = std::vector<Edge<T>>; template <class T> using Graph = std::vector<std::vector<Edge<T>>>; #line 4 "graph/dijkstra.hpp" template <class T> std::tuple<std::vector<T>, std::vector<int>, std::vector<int>> // dijkstra(Graph<T> &G, std::vector<int> &s, const T INF) { int N = (int)G.size(); std::vector<T> dist(N, INF); std::vector<int> par(N, -1), root(N, -1); std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> que; for (auto &v : s) { dist[v] = 0; root[v] = v; que.emplace(T(0), v); } while (!que.empty()) { auto [d, v] = que.top(); que.pop(); if (dist[v] != d) continue; // dist[v] < d for (auto &e : G[v]) { if (dist[e.to] > d + e.cost) { dist[e.to] = d + e.cost; root[e.to] = root[v]; par[e.to] = v; que.emplace(dist[e.to], e.to); } } } return {dist, par, root}; } #line 2 "graph/read_graph.hpp" #line 4 "graph/read_graph.hpp" template <class T> Graph<T> read_graph(const int n, const int m, const bool weight = false, const bool directed = false, const int offset = 1) { Graph<T> g(n); for (int i = 0; i < m; i++) { int a, b; std::cin >> a >> b; a -= offset, b -= offset; if (weight) { T c; std::cin >> c; if (!directed) g[b].push_back(Edge(b, a, c, i)); g[a].push_back(Edge(a, b, c, i)); } else { // c = 1 if (!directed) g[b].push_back(Edge(b, a, T(1), i)); g[a].push_back(Edge(a, b, T(1), i)); } } return g; } template <class T> Graph<T> read_parent(const int n, const bool weight = false, const bool directed = false, const int offset = 1) { Graph<T> g(n); for (int i = 1; i < n; i++) { int p; std::cin >> p; p -= offset; if (weight) { T c; std::cin >> c; if (!directed) g[i].push_back(Edge(i, p, c, i - 1)); g[p].push_back(Edge(p, i, c, i - 1)); } else { // c = 1 if (!directed) g[i].push_back(Edge(i, p, T(1), i - 1)); g[p].push_back(Edge(p, i, T(1), i - 1)); } } return g; } std::tuple<Graph<int>, std::vector<std::vector<int>>, std::vector<std::pair<int, int>>> read_grid(const int h, const int w, std::string rel = ".#") { std::vector<std::string> s(h); std::vector id(h, std::vector<int>(w, -1)); std::vector<std::pair<int, int>> loc; int n = 0; for (int i = 0; i < h; i++) { std::cin >> s[i]; for (int j = 0; j < w; j++) { if (s[i][j] == rel[1]) { id[i][j] = n++; loc.emplace_back(i, j); } } } int m = 0; Graph<int> g(n); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == rel[1]) { if (i + 1 < h and s[i + 1][j] == rel[1]) { g[id[i][j]].push_back(Edge(id[i][j], id[i + 1][j], 1, m)); g[id[i + 1][j]].push_back(Edge(id[i + 1][j], id[i][j], 1, m++)); } if (j + 1 < w and s[i][j + 1] == rel[1]) { g[id[i][j]].push_back(Edge(id[i][j], id[i][j + 1], 1, m)); g[id[i][j + 1]].push_back(Edge(id[i][j + 1], id[i][j], 1, m++)); } } } } return {g, id, loc}; } #line 7 "verify/lc_graph/lc_shortest_path_dijkstra.test.cpp" int main() { int N, M, s, t; std::cin >> N >> M >> s >> t; auto G = read_graph<long long>(N, M, true, true, 0); std::vector<int> ss = {s}; const long long INF = 1LL << 60; auto [d, p, r] = dijkstra(G, ss, INF); if (d[t] == INF) { std::cout << -1 << '\n'; return 0; } std::vector<int> ans; int c = t; while (t != -1) { ans.push_back(t); t = p[t]; } std::reverse(ans.begin(), ans.end()); std::cout << d[c] << ' ' << ans.size() - 1 << '\n'; for (int i = 0; i < ans.size() - 1; i++) std::cout << ans[i] << ' ' << ans[i + 1] << '\n'; return 0; }