제출 #1351240

#제출 시각아이디문제언어결과실행 시간메모리
1351240OwstinCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
310 ms23096 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <utility>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
#define pb push_back
#define all(x) begin(x), end(x)
#define space " "
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

void solve() {
    int n, m; cin >> n >> m;
    int s, t, u, v; cin >> s >> t >> u >> v; s--; t--; u--; v--;
    vector<vector<pair<int, ll>>> adj(n);
    for (int i = 0; i < m; i++) {
        int x, y, z; cin >> x >> y >> z; x--; y--;
        adj[x].pb({y, z});
        adj[y].pb({x, z});
    }
    auto dijkstra = [&] (int start) -> vector<ll> {
        vector<ll> res(n, LLONG_MAX);
        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
        pq.push({0, start});
        while (!pq.empty()) {
            auto curr = pq.top(); pq.pop();
            if (res[curr.second] <= curr.first) {
                continue;
            }
            res[curr.second] = curr.first;
            for (auto x : adj[curr.second]) {
                pq.push({x.second + curr.first, x.first});
            }
        }
        return res;
    };
    vector<ll> dists = dijkstra(s), distt = dijkstra(t), distu = dijkstra(u), distv = dijkstra(v);
    vector<bool> sp(n);
    for (int i = 0; i < n; i++) {
        if (dists[i] + distt[i] == dists[t]) {
            sp[i] = true;
        }
    }
    vector<pair<ll, ll>> res(n, {LLONG_MAX, LLONG_MAX});
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
    pq.push({0, s});
    vector<bool> vis(n);
    ll ans = distu[v];
    while (!pq.empty()) {
        auto curr = pq.top(); pq.pop();
        if (vis[curr.second]) {
            continue;
        }
        res[curr.second].first = min(res[curr.second].first, distu[curr.second]);
        res[curr.second].second = min(res[curr.second].second, distv[curr.second]);
        ans = min(ans, min(res[curr.second].first + distv[curr.second], res[curr.second].second + distu[curr.second]));
        vis[curr.second] = true;
        for (auto x : adj[curr.second]) {
            if (sp[x.first] && dists[x.first] == curr.first + x.second) {
                res[x.first].first = min(res[x.first].first, res[curr.second].first);
                res[x.first].second = min(res[x.first].second, res[curr.second].second);
                pq.push({curr.first + x.second, x.first});
            }
        }
    }
    cout << ans;
}

int main() {
    FAST_IO;
    //freopen("fcolor.in", "r", stdin);
    //freopen("fcolor.out", "w", stdout);
    //TEST_CASES;
    solve(); cout << endl;
    /*int a; cin >> a;
    for (int i = 1; i <= a; i++){
        cout << "Case #" << i << ": ";
        solve();
        cout << endl;
    }*/
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...