Submission #779931

#TimeUsernameProblemLanguageResultExecution timeMemory
779931andecaandeciCommuter Pass (JOI18_commuter_pass)C++17
0 / 100
268 ms35208 KiB
#include <bits/stdc++.h>
using namespace std;

# define int long long
# define fir first
# define sec second
# define pb push_back

const int cnst = 2e5+5;
bool mutipletestcase = 0;
//bool debug = false;

int n, m;
int s, t, u, v;
int par[cnst];
int shor[cnst];
int parshor[cnst];
int nxtshor[cnst];
vector<pair<int, int>> vec[cnst];

void djikstrast() {
    bool vis[n+5];
    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
    bool got = 0; int val = -1;
    pq.push({0, s, 0}); shor[s] = shor[t] = 1;
    memset(vis, 0, sizeof(vis));

    while(!pq.empty()) {
        auto[x, idx, parent] = pq.top(); pq.pop();
        // cerr << idx << endl; 

        if(idx == t) got = 1, val = x;
        if(got) {
            if(x != val) break;
            if(idx != t) continue;
            idx = parent;
            while(par[idx] != 0) {
                shor[idx] = 1;
                nxtshor[par[idx]] = idx;
                parshor[idx] = par[idx];
                idx = par[idx];
            }
            continue;
        }

        if(vis[idx]) continue;
        vis[idx] = 1;
        par[idx] = parent;

        for(auto v: vec[idx]) {
            if(!vis[v.fir]) pq.push({x+v.sec, v.fir, idx});
        }
    }

    // for(int i = 1; i<=n; i++) cerr << shor[i] << " "; cerr << endl;
}

void djikstrauv() {
    bool vis[n+5][5];
    memset(vis, 0, sizeof(vis));

    priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
    pq.push({0, u, 0});

    if(shor[u]) pq.push({0, u, 1}), pq.push({0, u, 2});

    while(!pq.empty()) {
        auto[x, idx, id] = pq.top(); pq.pop();

        if(vis[idx][id]) continue;
        vis[idx][id] = 1;

        // cerr << x << " " << idx << " " << id << endl;

        if(idx == v) {
            cout << x << endl; return;
        }

        if(id == 0) {
            for(auto v: vec[idx]) {
                if(!vis[v.fir][0]) pq.push({x+v.sec, v.fir, 0});
                if(shor[v.fir]) {
                    if(!vis[v.fir][1]) pq.push({x+v.sec, v.fir, 1});
                    if(!vis[v.fir][2]) pq.push({x+v.sec, v.fir, 2});
                }
            }
        }
        else if(id == 1) {
            int y = parshor[idx];
            if(!vis[y][1]) pq.push({x, y, 1});
            for(auto v: vec[idx]) if(!vis[v.fir][3]) pq.push({x+v.sec, v.fir, 3});
        }
        else if(id ==2) {
            int y = nxtshor[idx];
            if(!vis[y][2]) pq.push({x, y, 2});
            for(auto v: vec[idx]) {
                if(!vis[v.fir][3]) pq.push({x+v.sec, v.fir, 3});
            }
        }
        else {
            for(auto v: vec[idx]) if(!vis[v.fir][3]) pq.push({x+v.sec, v.fir, 3});
        }
    }
}
 
void solve() {
    cin >> n >> m >> s >> t >> u >> v;

    for(int i = 1; i<=m; i++) {
        int a, b, c; cin >> a >> b >> c;
        vec[a].pb({b, c});
        vec[b].pb({a, c});
    }

    djikstrast();
    djikstrauv();
}

signed main() {
    ios_base::sync_with_stdio(false);
    int t = 1;
    if(mutipletestcase) cin >> t; 
    while(t--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...