Submission #1169416

#TimeUsernameProblemLanguageResultExecution timeMemory
1169416AriadnaCommuter Pass (JOI18_commuter_pass)C++20
15 / 100
167 ms26552 KiB
#include <bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;

const int MAX_N = 1e5;
vector<pair<int, int>> adj[MAX_N];

vector<int> dijkstra(int n, int s) {
    vector<int> dist(n, 1e18);
    priority_queue<pair<int, int>> pq;
    pq.push({0, s});
    dist[s] = 0;
    while (!pq.empty()) {
        int d = -pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if (dist[u] != d) continue;

        for (pair<int, int> v: adj[u]) {
            if (dist[v.first] > dist[u]+v.second) {
                dist[v.first] = dist[u]+v.second;
                pq.push({-dist[v.first], v.first});
            }
        }
    }
    return dist;
}

vector<pair<int, vector<int>>> dijkstra2(int n, int s) {
    vector<pair<int, vector<int>>> dist(n, {1e18, {}});
    dist[s] = {0, {s}};
    priority_queue<pair<int, int>> pq;
    pq.push({0, s});
    while (!pq.empty()) {
        int d = -pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if (dist[u].first != d) continue;
        for (pair<int, int> v: adj[u]) {
            if (dist[v.first].first == dist[u].first+v.second) {
                dist[v.first].second.push_back(u);
            } else if (dist[v.first].first > dist[u].first+v.second) {
                dist[v.first] = {dist[u].first+v.second, {u}};
                pq.push({-dist[v.first].first, v.first});
            }
        }
    }
    return dist;
}

int ans;
vector<int> dist_u, dist_v;
vector<pair<int, vector<int>>> dist;

vector<bool> vis;
void calc_camino(int u, int s, int d1, int d2) {
    if (u == s || vis[u]) {
        ans = min(ans, d1+d2);
        return;
    }

    vis[u] = true;
    for (int v: dist[u].second) {
        calc_camino(v, s, min(d1, dist_u[v]), min(d2, dist_v[v]));
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;
    int s, t, u, v;
    cin >> s >> t >> u >> v;
    --s; --t; --u; --v;

    while (m--) {
        int a, b, c;
        cin >> a >> b >> c;
        --a; --b;
        adj[a].pb({b, c});
        adj[b].pb({a, c});
    }

    dist_u = dijkstra(n, u);
    dist_v = dijkstra(n, v);
    dist = dijkstra2(n, s);

    ans = dist_u[v];
    vis = vector<bool>(n, false);
    calc_camino(t, s, dist_u[t], dist_v[t]);
    cout << ans << endl;

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