Submission #375673

#TimeUsernameProblemLanguageResultExecution timeMemory
375673valerikkCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
435 ms22680 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll INF = (ll)1e18 + 100; struct Edge { int a, b, c; }; vector<ll> get_dists(int n, int st, const vector<vector<pair<int, int>>> &g) { vector<ll> dist(n, INF); dist[st] = 0; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; auto psh = [&](int v) { pq.push({dist[v], v}); }; psh(st); while (!pq.empty()) { auto [dst, v] = pq.top(); pq.pop(); if (dst != dist[v]) continue; for (const auto& [u, w] : g[v]) { if (dist[v] + w < dist[u]) { dist[u] = dist[v] + w; psh(u); } } } for (int i = 0; i < n; ++i) assert(dist[i] < INF); return dist; } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); int n, m, s, t, u, v; cin >> n >> m >> s >> t >> u >> v; --s; --t; --u; --v; vector<Edge> ed(m); vector<vector<pair<int, int>>> g(n); for (int i = 0; i < m; ++i) { cin >> ed[i].a >> ed[i].b >> ed[i].c; --ed[i].a; --ed[i].b; g[ed[i].a].push_back({ed[i].b, ed[i].c}); g[ed[i].b].push_back({ed[i].a, ed[i].c}); } auto ds = get_dists(n, s, g); auto dt = get_dists(n, t, g); auto du = get_dists(n, u, g); auto dv = get_dists(n, v, g); auto slv = [&]() { vector<pair<ll, int>> srt; for (int i = 0; i < n; ++i) srt.push_back({ds[i], i}); sort(srt.begin(), srt.end()); vector<ll> mn(n); for (int i = 0; i < n; ++i) mn[i] = dv[i]; ll ret = INF; for (int i = n - 1; i >= 0; --i) { int curv = srt[i].second; for (auto [curu, w] : g[curv]) { if (ds[curv] + dt[curu] + w == ds[t]) { mn[curv] = min(mn[curv], mn[curu]); } } ret = min(ret, du[curv] + mn[curv]); } return ret; }; ll ans = slv(); swap(u, v); swap(du, dv); ans = min(ans, slv()); cout << ans << endl; 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...