Submission #1351302

#TimeUsernameProblemLanguageResultExecution timeMemory
1351302sed9871Commuter Pass (JOI18_commuter_pass)C++20
0 / 100
1 ms2788 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll INF = 1e18;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<vector<pair<int,int>>> adj(N+1);

    for(int i = 0; i < M; i++){
        int a,b,c;
        cin >> a >> b >> c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }

    int S, T, U, V;
    cin >> S >> T >> U >> V;

    // Dijkstra from S
    vector<ll> dS(N+1, INF), dT(N+1, INF);

    auto dijkstra = [&](int src, vector<ll>& dist){
        priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<>> pq;
        dist[src] = 0;
        pq.push({0, src});
        while(!pq.empty()){
            auto [d,u] = pq.top(); pq.pop();
            if(d > dist[u]) continue;
            for(auto [v,w] : adj[u]){
                if(dist[v] > d + w){
                    dist[v] = d + w;
                    pq.push({dist[v], v});
                }
            }
        }
    };

    dijkstra(S, dS);
    dijkstra(T, dT);

    ll D = dS[T];

    // Build modified graph (0-weight edges if on some shortest S-T path)
    vector<vector<pair<int,ll>>> adj2(N+1);

    for(int u = 1; u <= N; u++){
        for(auto [v,w] : adj[u]){
            ll cost = w;
            if (dS[u] + w + dT[v] == D || dS[v] + w + dT[u] == D) {
                cost = 0;
            }
            adj2[u].push_back({v, cost});
        }
    }

    // Final Dijkstra from U to V
    vector<ll> dist(N+1, INF);
    priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<>> pq;

    dist[U] = 0;
    pq.push({0, U});

    while(!pq.empty()){
        auto [d,u] = pq.top(); pq.pop();
        if(d > dist[u]) continue;
        for(auto [v,w] : adj2[u]){
            if(dist[v] > d + w){
                dist[v] = d + w;
                pq.push({dist[v], v});
            }
        }
    }

    cout << dist[V] << "\n";

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