Submission #70905

#TimeUsernameProblemLanguageResultExecution timeMemory
70905someone_aaCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
1038 ms32216 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
using namespace std;
const int maxn = 100100;
vector<pii> g[maxn];
ll n, m, s, t, u, v;
ll x, y, z;

ll d[maxn][2];
ll dist[maxn][2];
ll dp[maxn][2];

void dijkstra() {
    for(int i=0;i<=n;i++) {
        d[i][0] = d[i][1] = LLONG_MAX;
    }
    d[u][0] = 0LL;
    priority_queue<pii, vector<pii>, greater<pii> > pq;
    pq.push(mp(0LL, u));
    while(!pq.empty()) {
        ll curr = pq.top().second;
        ll currd = pq.top().first;
        pq.pop();
        for(auto i:g[curr]) {
            ll nextd = currd + i.second;
            if(nextd < d[i.first][0]) {
                d[i.first][0] = nextd;
                pq.push(mp(d[i.first][0], i.first));
            }
        }
    }
    d[v][1] = 0LL;
    pq.push(mp(0LL, v));
    while(!pq.empty()) {
        ll curr = pq.top().second;
        ll currd = pq.top().first;
        pq.pop();
        for(auto i:g[curr]) {
            ll nextd = currd + i.second;
            if(nextd < d[i.first][1]) {
                d[i.first][1] = nextd;
                pq.push(mp(d[i.first][1], i.first));
            }
        }
    }
}

void solve() {
    for(int i=0;i<=n;i++) {
        dist[i][0] = dist[i][1] = LLONG_MAX;
        dp[i][0] = dp[i][1] = LLONG_MAX;
    }
    dist[s][0] = 0LL;
    priority_queue<pii, vector<pii>, greater<pii> > pq;
    pq.push(mp(0LL, s));
    dp[s][0] = d[s][1];

    while(!pq.empty()) {
        ll curr = pq.top().second;
        ll currd = pq.top().first;
        pq.pop();
        for(auto i:g[curr]) {
            ll nextd = currd + i.second;

            if(nextd < dist[i.first][0]) {
                dist[i.first][0] = nextd;
                dp[i.first][0] = min(dp[curr][0], d[i.first][1]);
                pq.push(mp(dist[i.first][0], i.first));;
            }
            else if(nextd == dist[i.first][0]) {
                dp[i.first][0] = min(dp[i.first][0], min(dp[curr][0], d[i.first][1]));
            }
        }
    }

    dist[t][1] = 0LL;
    dp[t][1] = d[t][1];
    pq.push(mp(0LL, t));

    while(!pq.empty()) {
        ll curr = pq.top().second;
        ll currd = pq.top().first;
        pq.pop();
        for(auto i:g[curr]) {
            ll nextd = currd + i.second;
            if(nextd < dist[i.first][1]) {
                dist[i.first][1] = nextd;
                dp[i.first][1] = min(dp[curr][1], d[i.first][1]);
                pq.push(mp(dist[i.first][1], i.first));;
            }
            else if(nextd == dist[i.first][1]) {
                dp[i.first][1] = min(dp[i.first][1], min(dp[curr][1], d[i.first][1]));
            }
        }
    }

    ll result = d[v][0];
    for(int i=1;i<=n;i++) {
        if(dist[i][0] + dist[i][1] == dist[t][0]) {
            result = min(result, d[i][0] + min(dp[i][0], dp[i][1]));
        }
    }
    cout<<result;
}

int main() {
    cin>>n>>m;
    cin>>s>>t;
    cin>>u>>v;
    for(int i=0;i<m;i++) {
        cin>>x>>y>>z;
        g[x].pb(mp(y,z));
        g[y].pb(mp(x,z));
    }

    dijkstra();
    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...