제출 #1047722

#제출 시각아이디문제언어결과실행 시간메모리
1047722inkvizytorCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
237 ms29124 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pli pair<ll, int>
#define fi first 
#define se second

ll MAX = 1e18;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, s, t, u, v;
    cin >> n >> m >> s >> t >> u >> v;
    vector<vector<pli>> g (n+1);
    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        g[a].push_back({c, b});
        g[b].push_back({c, a});
    }
    vector<ll> lu(n+1, -1), lv(n+1, -1), ls(n+1, -1);
    priority_queue<pli, vector<pli>, greater<pli>> pq;
    pq.push({0, u});
    while (!pq.empty()) {
        pli x = pq.top();
        pq.pop();
        if (lu[x.se] != -1) continue;
        lu[x.se] = x.fi;
        for (pli y : g[x.se]) {
            if (lu[y.se] == -1) {
                pq.push({x.fi+y.fi, y.se});
            }
        }
    }
    pq.push({0, v});
    while (!pq.empty()) {
        pli x = pq.top();
        pq.pop();
        if (lv[x.se] != -1) continue;
        lv[x.se] = x.fi;
        for (pli y : g[x.se]) {
            if (lv[y.se] == -1) {
                pq.push({x.fi+y.fi, y.se});
            }
        }
    }
    pq.push({0, s});
    while (!pq.empty()) {
        pli x = pq.top();
        pq.pop();
        if (ls[x.se] != -1) continue;
        ls[x.se] = x.fi;
        for (pli y : g[x.se]) {
            if (ls[y.se] == -1) {
                pq.push({x.fi+y.fi, y.se});
            }
        }
    }
    vector<vector<int>> G (n+1);
    vector<int> topo (1, s), k (n+1, 0);
    for (int i = 1; i < n+1; i++) {
        for (pli j : g[i]) {
            if (ls[i]+j.fi == ls[j.se]) G[i].push_back(j.se), k[j.se]++;
        }
    }
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
        int x = q.front(); q.pop();
        for (int i : G[x]) {
            k[i]--;
            if (!k[i]) topo.push_back(i), q.push(i);
        }
    }
    vector<ll> mu(n+1, MAX), mv(n+1, MAX);
    mu[s] = lu[s];
    mv[s] = lv[s]; 
    vector<ll> score(n+1, MAX);
    score[s] = mu[s]+mv[s];
    for (int i : topo) {
        score[i] = min ({score[i], mu[i]+lv[i], mv[i]+lu[i]});
        for (int x : G[i]) {
            score[x] = min(score[i], score[x]);
            mu[x] = min({mu[x], mu[i], lu[x]});
            mv[x] = min({mv[x], mv[i], lv[x]});
        }
    }
    cout << min(score[t], lu[v]) << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...