Submission #1193229

#TimeUsernameProblemLanguageResultExecution timeMemory
1193229vux2codeCommuter Pass (JOI18_commuter_pass)C++20
16 / 100
299 ms29236 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = numeric_limits<ll>::max() / 4;

// Standard Dijkstra to compute shortest paths
vector<ll> dijkstra(int src, const vector<vector<pair<int,ll>>> &adj) {
    int n = adj.size();
    vector<ll> dist(n, INF);
    using pli = pair<ll,int>;
    priority_queue<pli, vector<pli>, greater<pli>> pq;
    dist[src] = 0;
    pq.emplace(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.emplace(dist[v], v);
            }
        }
    }
    return dist;
}

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

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

    vector<vector<pair<int,ll>>> adj(N + 1);
    vector<tuple<int,int,ll>> edges;
    edges.reserve(M);
    for (int i = 0; i < M; ++i) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        adj[a].emplace_back(b, c);
        adj[b].emplace_back(a, c);
        edges.emplace_back(a, b, c);
    }

    // Compute shortest distances from S, T, U, and V
    auto dS = dijkstra(S, adj);
    auto dT = dijkstra(T, adj);
    auto dU = dijkstra(U, adj);
    auto dV = dijkstra(V, adj);
    ll bestST = dS[T];

    // Build DAG of edges on some shortest path from S to T
    vector<vector<int>> dag(N + 1), dag_rev(N + 1);
    for (auto &[a, b, c] : edges) {
        if (dS[a] + c + dT[b] == bestST) {
            dag[a].push_back(b);
            dag_rev[b].push_back(a);
        }
        if (dS[b] + c + dT[a] == bestST) {
            dag[b].push_back(a);
            dag_rev[a].push_back(b);
        }
    }

    // Mark nodes reachable from S and reaching T in the DAG
    vector<char> fromS(N + 1, false), toT(N + 1, false);
    queue<int> q;
    fromS[S] = true; q.push(S);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int v : dag[u]) {
            if (!fromS[v]) {
                fromS[v] = true;
                q.push(v);
            }
        }
    }
    toT[T] = true; q.push(T);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int v : dag_rev[u]) {
            if (!toT[v]) {
                toT[v] = true;
                q.push(v);
            }
        }
    }

    // Collect all nodes on some S-T shortest path
    vector<int> sp_nodes;
    for (int i = 1; i <= N; ++i) {
        if (fromS[i] && toT[i]) sp_nodes.push_back(i);
    }
    // Sort by distance from S to impose topological order
    sort(sp_nodes.begin(), sp_nodes.end(), [&](int a, int b) {
        return dS[a] < dS[b];
    });

    // DP: Bmin[u] = minimal distance dV[x] over all x reachable from u in DAG
    vector<ll> Bmin(N + 1, INF);
    for (int u : sp_nodes) {
        Bmin[u] = dV[u];
    }
    for (int i = (int)sp_nodes.size() - 1; i >= 0; --i) {
        int u = sp_nodes[i];
        for (int v : dag[u]) {
            if (toT[v]) {
                Bmin[u] = min(Bmin[u], Bmin[v]);
            }
        }
    }

    // Answer: either no pass (dU[V]) or boarding at some u and exiting at the best downstream node
    ll answer = dU[V];
    for (int u : sp_nodes) {
        if (dU[u] < INF && Bmin[u] < INF) {
            answer = min(answer, dU[u] + Bmin[u]);
        }
    }

    cout << answer << '\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...