제출 #579580

#제출 시각아이디문제언어결과실행 시간메모리
579580stevancvCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
380 ms30704 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> v(4);
    for (int i = 0; i < 4; i++) {
        cin >> v[i];
        v[i] -= 1;
    }
    vector<vector<pair<int, ll>>> g(n);
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a -= 1;
        b -= 1;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    vector<vector<ll>> dist(4, vector<ll>(n, 1e18));
    for (int i = 0; i < 4; i++) {
        priority_queue<pair<ll, int>> pq;
        vector<bool> was(n);
        pq.push({0, v[i]});
        dist[i][v[i]] = 0;
        while (!pq.empty()) {
            auto s = pq.top();
            pq.pop();
            s.first *= -1;
            if (was[s.second] == 1) continue;
            was[s.second] = 1;
            for (auto u : g[s.second]) {
                if (dist[i][u.first] > s.first + u.second) {
                    dist[i][u.first] = s.first + u.second;
                    pq.push({-dist[i][u.first], u.first});
                }
            }
        }
    }
    vector<vector<int>> dag(n);
    for (int i = 0; i < n; i++) {
        for (auto u : g[i]) {
            if (dist[0][i] + u.second + dist[1][u.first] == dist[0][v[1]]) dag[i].push_back(u.first);
        }
    }
    ll ans = dist[2][v[3]];
    vector<bool> was(n);
    vector<ll> dp(n);
    function<void(int, int)> Dfs = [&] (int s, int ty) {
        if (was[s] == 1) return;
        was[s] = 1;
        dp[s] = dist[2 + ty][s];
        for (int u : dag[s]) {
            Dfs(u, ty);
            smin(dp[s], dp[u]);
        }
        smin(ans, dist[3 - ty][s] + dp[s]);
    };
    for (int z = 0; z < 2; z++) {
        for (int i = 0; i < n; i++) {
            was[i] = 0;
            dp[i] = 1e18;
        }
        Dfs(v[0], z);
    }
    cout << ans << en;
    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...