Submission #530984

#TimeUsernameProblemLanguageResultExecution timeMemory
530984john_wickCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
503 ms30892 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long int #define mod 1000000007 #define pb push_back #define ff first #define ss second typedef pair<int,int> pp; bool com(pp x, pp y){ if(x.ff == y.ff) return x.ss < y.ss; return x.ff < y.ff; } ll power(ll x, ll y){ ll prod = 1; while(y){ if(y & 1) prod = (prod * x) % mod; x = (x * x) % mod; y /= 2; } return prod; } const int N = 2e5 + 7; int tc = 1; vector<pair<int, ll>> vtx[N]; ll ds[N]; void dijsktra(int s, ll *d, int n){ vector<int> vis(n + 1); priority_queue<pair<ll, int>> q; q.push({0, s}); while(!q.empty()){ ll c = q.top().ff; int u = q.top().ss; q.pop(); if(!vis[u]){ d[u] = -c; vis[u] = 1; for(auto v : vtx[u]){ q.push({c - v.ss, v.ff}); } } } } void dijstra2(int s, int t, ll *du, ll *dv, int n, ll &ans){ vector<int> vis(n + 1); ll dp[2][n + 2]; for(int i = 0; i <= n; i++) dp[0][i] = dp[1][i] = 1e15; priority_queue<pair<ll, pair<int, int>>> q; q.push({0, {s, 0}}); while(!q.empty()){ ll c; int node, par; c = q.top().first; node = q.top().second.first; par = q.top().second.second; q.pop(); if (!vis[node]) { vis[node] = 1; ds[node] = -c; dp[0][node] = min(du[node], dp[0][par]); dp[1][node] = min(dv[node], dp[1][par]); for (auto v : vtx[node]) q.push({c - v.second, {v.first, node}}); } else if (-c == ds[node]) { if (min(du[node], dp[0][par]) + min(dv[node], dp[1][par]) <= dp[0][node] + dp[1][node]) { dp[0][node] = min(du[node], dp[0][par]); dp[1][node] = min(dv[node], dp[1][par]); } } } ans = min(ans, dp[0][t] + dp[1][t]); } void solve(){ int n, m; cin >> n >> m; int s, t; cin >> s >> t; int u, v; cin >> u >> v; for(int i = 0; i < m; i++){ int uu, vv; ll w; cin >> uu >> vv >> w; vtx[uu].pb({vv, w}); vtx[vv].pb({uu, w}); } ll du[n + 1], dv[n + 1]; dijsktra(u, du, n); dijsktra(v, dv, n); ll ans = du[v]; dijstra2(s, t, du, dv, n, ans); dijstra2(t, s, du, dv, n, ans); cout << ans << "\n"; // cout << "Case #" << tc << ": " << ans + k << "\n"; // tc++; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); 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...