Submission #1130417

#TimeUsernameProblemLanguageResultExecution timeMemory
1130417akzytrCommuter Pass (JOI18_commuter_pass)C++20
16 / 100
529 ms28228 KiB
#include <bits/stdc++.h>
#define ve vector
#define ar array 
#define pb push_back
#define ins insert

#define endl '\n'
#define ll long long
using namespace std;


const int MXN = 2e5+1;
int N, M;

int S, T;

int U, V;

ve<pair<int, ll>> adj[MXN];
void subtask1(){
    set<ar<ll, 3>> pq;
    ve<ll> distv(N+1, 1e18);
    pq.insert({0, -1, V});
    while(!pq.empty()){
        auto [cst, _, sor] = *pq.begin();
        pq.erase(pq.begin());

        if(cst < distv[sor]){
            distv[sor] = cst;
            for(auto [x, c] : adj[sor]){
                pq.insert({cst + c, _, x});
            }
        }
    }
    ve<ll> dists(N+1, 1e18);
    ve<ll> ans(N+1, distv[S]+1);
    pq.insert({0, distv[S], S});
    while(!pq.empty()){
        auto [cst, rel, sor] = *pq.begin();
        pq.erase(pq.begin());

        if(cst <= dists[sor] && rel < ans[sor]){
            dists[sor] = cst;
            ans[sor] = rel;
            for(auto [x, c] : adj[sor]){
                ll n_rel = min(rel, distv[x]);
                pq.insert({cst + c, n_rel, x});
            }
        }
    }
    cout << ans[T] << endl;
}

void subtask2(){
    map<pair<int, int>, bool> use_edge;

    ve<ll> dsts(N+1, 1e18);
    ve<ll> p(N+1, 0);

    set<pair<ll, int>> pq;
    pq.insert({0, S});
    while(!pq.empty()){
        auto top = *pq.begin();
        pq.erase(pq.begin());
        if(top.first < dsts[top.second]){
            dsts[top.second] = top.first;
            for(auto [x, c] : adj[top.second]){
                if(top.first + c < dsts[x]){
                    p[x] = top.second;
                    pq.insert({top.first+c, x});
                }
            }
        }
    }

    ve<ll> path;
    int x = T;

    while(x != S){
        path.pb(x);
        x = p[x];
    }

    path.pb(S);
    reverse(path.begin(), path.end());
    for(int i  = 0; i < path.size()-1; i++){
        adj[path[i]].pb({path[i+1], 0});
        adj[path[i+1]].pb({path[i], 0});
    }

    ve<ll> dstv(N+1, 1e18);

    pq.insert({0, V});
    while(!pq.empty()){
        auto top = *pq.begin();
        pq.erase(pq.begin());
        if(top.first < dstv[top.second]){
            dstv[top.second] = top.first;
            for(auto [x, c] : adj[top.second]){
                if(top.first + c < dsts[x]){
                    p[x] = top.second;
                    pq.insert({top.first+c, x});
                }
            }
        }
    }
    cout << dstv[U] << endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    cin >> S >> T;
    cin >> U >> V;
    for(int i = 0; i < M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].pb({b, c});
        adj[b].pb({a, c});
    }

    
    if(S==U){
        subtask1();
    }
    else{
        subtask2();
    }

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...