Submission #482587

#TimeUsernameProblemLanguageResultExecution timeMemory
482587jalsolCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
253 ms25264 KiB
#ifdef LOCAL #include "local.h" #else #include <bits/stdc++.h> #define debug(...) #define DB(...) #endif using namespace std; const bool __initialization = []() { cin.tie(nullptr)->sync_with_stdio(false); #define TASK "empty" if (fopen(TASK".inp", "r")) { (void)(!freopen(TASK".inp", "r", stdin)); (void)(!freopen(TASK".out", "w", stdout)); } cout << setprecision(12) << fixed; return true; }(); using ll = long long; using ld = long double; #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define For(i, l, r) for (int i = (l); i <= (r); ++i) #define Ford(i, r, l) for (int i = (r); i >= (l); --i) #define Rep(i, n) For (i, 0, (n) - 1) #define Repd(i, n) Ford (i, (n) - 1, 0) #define Fe(...) for (auto __VA_ARGS__) template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); } template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; } template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; } constexpr ld eps = 1e-9; constexpr int inf = 1e9; constexpr ll linf = 1e18; // ============================================================================= constexpr int maxn = 1e5 + 5; struct Link { int v, c; }; struct Data { int u; ll du; bool operator<(const Data& o) const { return du > o.du; } }; int n, ne; int src, dest; int U, V; vector<Link> g[maxn]; ll ds[maxn], dU[maxn], dV[maxn]; priority_queue<Data> q; void Dijkstra(int s, ll* d) { memset(d + 1, 0x3f, sizeof(ll) * n); d[s] = 0; q.push({s, d[s]}); while (isz(q)) { auto [u, du] = q.top(); q.pop(); if (du != d[u]) continue; Fe (&[v, c] : g[u]) { if (chmin(d[v], d[u] + c)) { q.push({v, d[v]}); } } } } ll ans; bool vis[maxn], visDest[maxn]; ll dpU[maxn], dpV[maxn]; void Dfs(int u) { if (vis[u]) return; vis[u] = true; dpU[u] = dpV[u] = linf; if (u == dest) visDest[u] = true; Fe (&[v, c] : g[u]) { if (ds[v] != ds[u] + c) continue; Dfs(v); if (visDest[v]) { visDest[u] = true; chmin(dpU[u], dpU[v]); chmin(dpV[u], dpV[v]); } } if (visDest[u]) { chmin(ans, dpU[u] + dV[u]); chmin(ans, dpV[u] + dU[u]); chmin(dpU[u], dU[u]); chmin(dpV[u], dV[u]); } } signed main() { cin >> n >> ne; cin >> src >> dest; cin >> U >> V; Rep (_, ne) { int u, v, c; cin >> u >> v >> c; g[u].push_back({v, c}); g[v].push_back({u, c}); } Dijkstra(src, ds); Dijkstra(U, dU); Dijkstra(V, dV); ans = dU[V]; Dfs(src); cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...