Submission #368061

#TimeUsernameProblemLanguageResultExecution timeMemory
368061Haruto810198Commuter Pass (JOI18_commuter_pass)C++17
15 / 100
2084 ms126832 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define double long double

#define FOR(i,l,r,d) for(int i=(l);i<=(r);i+=(d))

#define vi vector<int>
#define pii pair<int,int>

#define F first
#define S second

#define pb push_back
#define eb emplace_back
#define mkp make_pair

const int INF=2147483647;
const int MOD=1000000007;
const int mod=998244353;
const double eps=1e-12;

int n,m;
int S,T,U,V;
vector<pii> edge[100001];

bool vis[100001];
int ds[100001],du[100001],dv[100001];
vi spt[100001];

int res;

void Dijkstra(int from,int dis[]){

    priority_queue< pii, vector<pii>, greater<pii> > pq;

    FOR(i,0,n-1,1){
        dis[i] = INF*INF;
        vis[i] = 0;
    }
    dis[from] = 0;
    pq.emplace(0,from);

    while( !pq.empty() ){

        int v = pq.top().S;
        pq.pop();

        if( vis[v] == 1 ){
            continue;
        }

        vis[v] = 1;

        for(pii i : edge[v]){
            if( dis[v]+i.S < dis[i.F] ){
                dis[i.F] = dis[v]+i.S;
                pq.emplace(dis[i.F],i.F);
            }
        }

    }

}

int indeg[100001];
int dpu[100001],dpv[100001];
vi qu;
vi dporder;

void solve(){

    FOR(i,0,n-1,1){
        indeg[i] = 0;
    }

    FOR(i,0,n-1,1){
        for(int j : spt[i]){
            indeg[j]++;
        }
    }

    FOR(i,0,n-1,1){
        if(indeg[i]==0){
            qu.pb(i);
        }
    }

    while( !qu.empty() ){

        int v = qu.back();
        qu.pop_back();
        dporder.pb(v);

        for(int i : spt[v]){
            indeg[i]--;
            if(indeg[i]==0){
                qu.pb(i);
            }
        }

    }

    FOR(i,0,n-1,1){
        dpu[i] = du[i];
        dpv[i] = dv[i];
    }

    for(int i : dporder){
        for(int j : spt[i]){
            if( min( dpu[i] , du[j] ) + min( dpv[i] , dv[j] ) < dpu[j] + dpv[j] ){
                dpu[j] = min( dpu[i] , du[j] );
                dpv[j] = min( dpv[i] , dv[j] );
            }
        }
    }
    /*
    cerr<<"dpu : ";
    FOR(i,0,n-1,1){
        if(dpu[i]>100) cerr<<"- ";
        else cerr<<dpu[i]<<" ";
    }
    cerr<<endl;
    cerr<<"dpv : ";
    FOR(i,0,n-1,1){
        if(dpv[i]>100) cerr<<"- ";
        else cerr<<dpv[i]<<" ";
    }
    cerr<<endl;
    */
    res = min( res , dpu[T] + dpv[T] );

}

signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m>>S>>T>>U>>V;
    S--;
    T--;
    U--;
    V--;

    FOR(i,0,m-1,1){
        int v1,v2,w;
        cin>>v1>>v2>>w;
        v1--;
        v2--;
        edge[v1].eb(v2,w);
        edge[v2].eb(v1,w);
    }

    Dijkstra(S,ds);
    Dijkstra(U,du);
    Dijkstra(V,dv);
    /*
    cerr<<"ds : ";
    FOR(i,0,n-1,1){
        cerr<<ds[i]<<" ";
    }
    cerr<<endl;
    cerr<<"du : ";
    FOR(i,0,n-1,1){
        cerr<<du[i]<<" ";
    }
    cerr<<endl;
    cerr<<"dv : ";
    FOR(i,0,n-1,1){
        cerr<<dv[i]<<" ";
    }
    cerr<<endl;
    */
    res = du[V];

    vi sptqu;
    sptqu.pb(T);
    while( !sptqu.empty() ){
        int v = sptqu.back();
        sptqu.pop_back();
        for(pii i : edge[v]){
            if( ds[i.F] + i.S == ds[v] ){
                spt[i.F].pb(v);
                sptqu.pb(i.F);
            }
        }
    }
    /*
    cerr<<"spt : "<<endl;
    FOR(i,0,n-1,1){
        cerr<<"spt["<<i<<"] : ";
        for(int j : spt[i]){
            cerr<<j<<" ";
        }
        cerr<<endl;
    }
    cerr<<endl;
    */
    solve();

    swap(U,V);
    FOR(i,0,n-1,1){
        swap(du[i],dv[i]);
    }

    solve();

    cout<<res<<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...