제출 #1289359

#제출 시각아이디문제언어결과실행 시간메모리
1289359peanutCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
156 ms20164 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
template<class X, class Y> bool maximize (X &x, const Y &y) {return x < y ? x = y, 1 : 0;}
template<class X, class Y> bool minimize (X &x, const Y &y) {return x > y ? x = y, 1 : 0;}
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int n, m, a, b, c, d;
vector<pii> adj[maxn];

vector<ll> dijkstra(int s) {
    vector<ll> dist(n+1, LLONG_MAX);
    dist[s] = 0;
    priority_queue<pii> pq;
    pq.push({0, s});
    while (!pq.empty()) {
        ll d = -pq.top().fi;
        int u = pq.top().se;
        pq.pop();
        if (d != dist[u]) continue;
        for (auto v : adj[u]) {
            if (minimize(dist[v.fi], d + v.se)) pq.push({-dist[v.fi], v.fi});
        }
    }
    return dist;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    //freopen(".INP", "r", stdin);
    //freopen(".OUT", "w", stdout);
    cin >> n >> m >> a >> b >> c >> d;
    for (int i = 1; i <= m; ++i) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }
    vector<ll> distC = dijkstra(c);
    vector<ll> distD = dijkstra(d);
    vector<ll> mindistC(n+1, LLONG_MAX); // mindist from c to any node on shortest path from a to i
    vector<ll> mindistD(n+1, LLONG_MAX); // mindist from d to any node on shortest path from a to i
    vector<ll> dist(n+1, LLONG_MAX);
    dist[a] = 0;
    mindistC[a] = distC[a];
    mindistD[a] = distD[a];
    priority_queue<pii> pq;
    pq.push({0, a});

    while (!pq.empty()) {
        ll d = -pq.top().fi;
        int u = pq.top().se;
        pq.pop();
        if (dist[u] != d) continue;
        for (auto v : adj[u]) {
            if (minimize(dist[v.fi], v.se + d)) {
                mindistC[v.fi] = min(mindistC[u], distC[v.fi]);
                mindistD[v.fi] = min(mindistD[u], distD[v.fi]);
                pq.push({-dist[v.fi], v.fi});
            }
            else if (dist[v.fi] == v.se + d) {
                if (min(mindistC[u], distC[v.fi]) + min(mindistD[u], distD[v.fi]) < mindistC[v.fi] + mindistD[v.fi]) {
                    mindistC[v.fi] = min(mindistC[u], distC[v.fi]);
                    mindistD[v.fi] = min(mindistD[u], distD[v.fi]);
                    pq.push({-dist[v.fi], v.fi});
                }
            }
        }
    }
    cout << min(distC[d], mindistC[b] + mindistD[b]);
    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...