Submission #1332078

#TimeUsernameProblemLanguageResultExecution timeMemory
1332078bhira.piromsopaCommuter Pass (JOI18_commuter_pass)C++20
24 / 100
2094 ms10848 KiB
#include <bits/stdc++.h>

using namespace std;

using P = pair<int,int>;
using ll = long long;

const int N=1e5+5;
const int M=2e5+6;
const ll INF = 1e18;

int n,m;
int s1,s2;
int t1,t2;


vector<P> adj[N];

ll distsc[N],dist1[N],dist2[N];
ll dp[N][2][2];

void djisktra(int a, ll* dist) {
    priority_queue<pair<ll,int>> pq;
    for(int i=1; i<=n; i++) {
        dist[i] = INF;
    }
    dist[a] = 0;
    pq.emplace(0,a);
    while(!pq.empty()) {
        auto [w,u] = pq.top();
        pq.pop();
        if(w > dist[u]) {
            continue;
        }
        for(auto [v,d] : adj[u]) {
            // cerr << u << "->" << v << " " << d << '\n';
            if(dist[v] > w+d) {
                dist[v] = w+d;
                pq.emplace(dist[v],v);
            }
        }
    }
}


int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> m;
    cin >> s1 >> s2;
    cin >> t1 >> t2;

    int u,v,w;
    for(int i=0; i<m; i++) {
        cin >> u >> v >> w;
        adj[u].emplace_back(v,w);
        adj[v].emplace_back(u,w);
    }

    djisktra(s1,distsc);
    djisktra(t1,dist1);
    djisktra(t2,dist2);

    vector<int> topo;
    for(int i=1; i<=n; i++) {
        topo.emplace_back(i);
    }
    sort(topo.begin(),topo.begin()+n, [](int a,int b) {
        return distsc[a] < distsc[b];
    });
    
    // for(int i=0; i<n; i++) {
    //     cerr << topo[i] << ' ';
    // }
    // cerr << '\n';

    for(int cur = 1; cur <= n; cur++) {
        for(int x=0; x<2; x++) {
            for(int y=0; y<2; y++) {
                dp[cur][x][y] = INF;
            }
        }
    }

    dp[s1][0][0] = 0;

    for (int cur : topo) {
        for(int x = 0 ; x < 2; x++) {
            for(int y = 0; y < 2; y++) {
                if(x==0) {
                    // If haven't connected to house
                    dp[cur][1][y] = min(dp[cur][1][y],dp[cur][x][y] + dist1[cur]);
                }
                if(y==0) {
                    // If haven't connected to Library
                    dp[cur][x][1] = min(dp[cur][x][1],dp[cur][x][y] + dist2[cur]);
                }
            }
        }
        // See which way should take train to work;
        for (auto [v,w] : adj[cur]) {
            if(distsc[v] != distsc[cur]+w) continue; /*Did not take this path*/
            for(int x=0; x<2; x++){
                for(int y=0; y<2; y++) {
                    dp[v][x][y] = min(dp[v][x][y], dp[cur][x][y]);
                }
            }
        }
    }

    cout << min(dp[s2][1][1],dist1[t2]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...