제출 #479838

#제출 시각아이디문제언어결과실행 시간메모리
479838ponytailCommuter Pass (JOI18_commuter_pass)C++17
16 / 100
464 ms20504 KiB
#include<bits/stdc++.h>
#define int long long 
using namespace std;
signed main() {
    int N, M, S, T, U, V; cin >> N >> M >> S >> T >> U >> V;
    vector<pair<int,int> > adj[N+1];
    for(int i=0; i<M; i++) {
        int a, b, c; cin >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
    }
    int dist[N+1]; int vis[N+1];
    for(int i=1; i<=N; i++) dist[i] = 1e18, vis[i] = 0;
    dist[S] = 0;
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
    for(int i=1; i<=N; i++) pq.push({dist[i], i});
    while(pq.size()) {
        pair<int,int> t=pq.top(); pq.pop();
        if(!vis[t.second]) {
            vis[t.second] = 1;
            for(auto x : adj[t.second]) {
                if(!vis[x.first] && dist[x.first] > dist[t.second] + x.second) {
                    dist[x.first] = dist[t.second] + x.second;
                    pq.push({dist[x.first], x.first});
                }
            }
        }
    }
    int dist2[N+1]; int vis2[N+1];
    for(int i=1; i<=N; i++) dist2[i] = 1e18, vis2[i] = 0;
    dist2[T] = 0;
    for(int i=1; i<=N; i++) pq.push({dist2[i], i});
    while(pq.size()) {
        pair<int,int> t=pq.top(); pq.pop();
        if(!vis2[t.second]) {
            vis2[t.second] = 1;
            for(auto x : adj[t.second]) {
                if(!vis2[x.first] && dist2[x.first] > dist2[t.second] + x.second) {
                    dist2[x.first] = dist2[t.second] + x.second;
                    pq.push({dist2[x.first], x.first});
                }
            }
        }
    }
    int dist3[N+1]; int vis3[N+1];
    for(int i=1; i<=N; i++) dist3[i] = 1e18, vis3[i] = 0;
    dist3[V] = 0;
    for(int i=1; i<=N; i++) pq.push({dist3[i], i});
    while(pq.size()) {
        pair<int,int> t=pq.top(); pq.pop();
        if(!vis3[t.second]) {
            vis3[t.second] = 1;
            for(auto x : adj[t.second]) {
                if(!vis3[x.first] && dist3[x.first] > dist3[t.second] + x.second) {
                    dist3[x.first] = dist3[t.second] + x.second;
                    pq.push({dist3[x.first], x.first});
                }
            }
        }
    }
    int ans = dist[V];
    for(int i=1; i<=N; i++) {
        if(dist[i] + dist2[i] == dist[T]) {
            ans = min(ans, dist3[i]);
        }
    }
    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...