Submission #791775

#TimeUsernameProblemLanguageResultExecution timeMemory
791775kebineCommuter Pass (JOI18_commuter_pass)C++17
15 / 100
2070 ms262144 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second

int a[4];
ll dist[4][100005];
vector<pair<int, ll>> adj[100005];
priority_queue<pair<ll, int>> pq;
queue<tuple<int, ll, ll>> q;

void bfs(int type) {
    pq.push({0, a[type]});
    while (!pq.empty()) {
        int x = pq.top().se;
        ll d = -pq.top().fi;
        pq.pop();
        if (dist[type][x] != d) continue;
        for (auto u : adj[x]) if (d + u.se < dist[type][u.fi]) dist[type][u.fi] = d + u.se, pq.push({-(d + u.se), u.fi}); 
    }
}

int main() { 
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < 4; i++) cin >> a[i];
    for (int i = 0; i < 4; i++) {
        for (int j = 1; j <= n; j++) if (j != a[i]) dist[i][j] = 1e18;
    }
    for (int i = 0; i < m; i++) {
        int x, y;
        ll w;
        cin >> x >> y >> w;
        adj[x].push_back({y, w});
        adj[y].push_back({x, w});
    }
    for (int i = 0; i < 4; i++) bfs(i);
    ll ans = dist[2][a[3]];
    q.push({a[0], dist[0][a[2]], dist[0][a[3]]});
    while (!q.empty()) {
        int x;
        ll y, z;
        tie(x, y, z) = q.front();
        q.pop();
        ans = min(ans, y + z);
        for (auto u : adj[x]) {
            if (dist[0][x] + u.se + dist[1][u.fi] != dist[0][a[1]] or dist[0][x] >= dist[0][u.fi]) continue;
            q.push({u.fi, min(y, dist[2][u.fi]), min(z, dist[3][u.fi])});
        }
    }
    cout << ans << "\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...