Submission #1308193

#TimeUsernameProblemLanguageResultExecution timeMemory
1308193vaishakhvCommuter Pass (JOI18_commuter_pass)C++20
16 / 100
472 ms35208 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll cap = 2e5+1;

vector<pair<ll,ll>> adj[cap];
vector<ll> dag[cap];
ll indegree[cap];

ll distS[cap];
ll distU[cap];
ll distV[cap];

vector<tuple<ll,ll,ll>> edges;
priority_queue<pair<ll,ll>> pqs;
priority_queue<pair<ll,ll>> pqu;
priority_queue<pair<ll,ll>> pqv;

ll s, t, u, v;
ll n, m;

void dijkstra(){
    pqs.push({0,s});
    while(!pqs.empty()){
        auto [nd, x] = pqs.top(); pqs.pop();
        ll d = -nd;
        if (d != distS[x]) continue;
        for (auto [y,w]: adj[x]){
            if (distS[x] + w < distS[y]){
                distS[y] = distS[x] + w;
                pqs.push({-distS[y], y});
            }
        }
    }

    pqu.push({0,u});
    while(!pqu.empty()){
        auto [nd, x] = pqu.top(); pqu.pop();
        ll d = -nd;
        if (d != distU[x]) continue;
        for (auto [y,w]: adj[x]){
            if (distU[x] + w < distU[y]){
                distU[y] = distU[x] + w;
                pqu.push({-distU[y], y});
            }
        }
    }

    pqv.push({0,v});
    while(!pqv.empty()){
        auto [nd, x] = pqv.top(); pqv.pop();
        ll d = -nd;
        if (d != distV[x]) continue;
        for (auto [y,w]: adj[x]){
            if (distV[x] + w < distV[y]){
                distV[y] = distV[x] + w;
                pqv.push({-distV[y], y});
            }
        }
    }
}

ll run(){
    fill(distS, distS+cap, 1e18);
    fill(distU, distU+cap, 1e18);
    fill(distV, distV+cap, 1e18);
    fill(indegree, indegree+cap, 0);

    for (ll i=1;i<=n;i++) dag[i].clear();
    while(!pqs.empty()) pqs.pop();
    while(!pqu.empty()) pqu.pop();
    while(!pqv.empty()) pqv.pop();

    distS[s] = 0;
    distU[u] = 0;
    distV[v] = 0;

    dijkstra();

    for (auto [a,b,w]: edges){
        if (distS[a] + w == distS[b]){
            dag[a].push_back(b);
            indegree[b]++;
        }
        if (distS[b] + w == distS[a]){
            dag[b].push_back(a);
            indegree[a]++;
        }
    }

    queue<ll> q;
    vector<ll> topo;
    for (ll i=1;i<=n;i++){
        if (indegree[i]==0 && distS[i]!=1e18) q.push(i);
    }

    while(!q.empty()){
        ll x=q.front(); q.pop();
        topo.push_back(x);
        for(ll y:dag[x]){
            if(--indegree[y]==0) q.push(y);
        }
    }

    vector<ll> dp_entry(n+1,1e18), dp_ans(n+1,1e18);

    for(ll x:topo){
        dp_entry[x]=min(dp_entry[x],distU[x]);
        dp_ans[x]=min(dp_ans[x],dp_entry[x]+distV[x]);
        for(ll y:dag[x]){
            dp_entry[y]=min(dp_entry[y],dp_entry[x]);
            dp_ans[y]=min(dp_ans[y],dp_ans[x]);
        }
    }

    return min(distU[v], dp_ans[t]);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>m;
    cin>>s>>t>>u>>v;

    for(ll i=0;i<m;i++){
        ll a,b,w; cin>>a>>b>>w;
        adj[a].push_back({b,w});
        adj[b].push_back({a,w});
        edges.push_back({a,b,w});
    }

    ll ans1 = run();
    swap(s,t);
    swap(u,v);
    ll ans2 = run();

    cout << min(ans1, ans2);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...