제출 #852255

#제출 시각아이디문제언어결과실행 시간메모리
852255CalicoCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
783 ms92236 KiB
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; vector<long long> dijkstra(int n, vector<tuple<int, int, int>> edges, int src) { vector<vector<pair<int, int>>> g(n); for (auto [x, y, w]: edges) { g[x].emplace_back(y, w); } vector<long long> dist(n, INF); priority_queue<pair<long long, int>> pq; pq.push({0, src}); dist[src] = 0; while (pq.size()) { auto [ds, u] = pq.top(); pq.pop(); if (dist[u] != -ds) continue; for (auto [v, w]: g[u]) { if (dist[v] > dist[u]+w) { dist[v] = dist[u]+w; pq.push({-dist[v], v}); } } } return dist; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, s, t, u, v; cin >> n >> m >> s >> t >> u >> v; s--, t--, u--, v--; vector<tuple<int, int, int>> g1; for (int i = 0; i < m; i++) { int x, y, w; cin >> x >> y >> w; x--, y--; g1.emplace_back(x, y, w); g1.emplace_back(y, x, w); } vector<long long> dists = dijkstra(n, g1, s); vector<long long> distt = dijkstra(n, g1, t); vector<tuple<int, int, int>> g2; for (int i = 0; i < n; i++) { g2.emplace_back(i, n+i, 0); g2.emplace_back(i, 2*n+i, 0); g2.emplace_back(n+i, 3*n+i, 0); g2.emplace_back(2*n+i, 3*n+i, 0); } for (auto [x, y, w]: g1) { g2.emplace_back(x, y, w); g2.emplace_back(3*n+x, 3*n+y, w); if (dists[x]+distt[y]+w == dists[t]) { g2.emplace_back(n+x, n+y, 0); g2.emplace_back(2*n+y, 2*n+x, 0); } } vector<long long> ans = dijkstra(4*n, g2, u); cout << ans[3*n+v] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...