제출 #521260

#제출 시각아이디문제언어결과실행 시간메모리
521260NetrCommuter Pass (JOI18_commuter_pass)C++14
31 / 100
459 ms37108 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;

#ifdef DEBUG
#define D(x...) printf(x);
#else
#define D(x...)
#endif

const ll MAXN = 1e5 + 5;
const ll INF = 1ll<<62;

ll N,M,S,T,U,V,D[MAXN],vis[MAXN];

vector<pi> edge[MAXN];
vector<ll> succ[MAXN], pred[MAXN];

void dfs(int i){
    vis[i] = 1;
    if(i == S) return;
    for(auto e : edge[i]){
        if(D[e.first] + e.second == D[i]){
            succ[e.first].push_back(i);
            pred[i].push_back(e.first);
            if(!vis[e.first]) dfs(e.first);
        }
    }
}

int main(){
    cin >> N >> M >> S >> T >> U >> V;
    for(int i = 0; i < M; i++){
        ll ai, bi, ci;
        cin >> ai >> bi >> ci;
        edge[bi].push_back({ai, ci});
        edge[ai].push_back({bi, ci});
    }
    
    priority_queue<pi, vector<pi>, greater<pi>> pq;
    fill(begin(D), end(D), INF);
    D[S] = 0;
    pq.push({0, S});
    while(!pq.empty()){
        pi t = pq.top(); pq.pop();
        if(t.first != D[t.second]) continue;
        if(t.second == T) break;
        for(auto e : edge[t.second]){
            if(D[t.second] + e.second < D[e.first]){
                pq.push({D[e.first] = D[t.second] + e.second, e.first});
            }
        }
    }
    dfs(T);

    using ti = tuple<ll,ll,ll>;

    priority_queue<ti, vector<ti>, greater<ti>> pq2;
    fill(begin(D), end(D), INF);
    D[U] = 0;
    pq2.push({0, U, 0});
    while(!pq2.empty()){
        ti t = pq2.top(); pq2.pop();
        ll dist = get<0>(t), curr = get<1>(t), state = get<2>(t);
        D("%d %d %d\n", dist, curr, state);
        if(dist != D[curr]) continue;
        if(curr == V) break;
        if(state != -1){
            for(auto e : succ[curr]){
                if(dist < D[e]){
                    pq2.push({D[e] = dist, e, 1});
                }
            }
            for(auto e : edge[curr]){
                if(dist + e.second < D[e.first]){
                    pq2.push({D[e.first] = dist + e.second, e.first, -state});
                }
            }
        }else{
            for(auto e : edge[curr]){
                if(dist + e.second < D[e.first]){
                    pq2.push({D[e.first] = dist + e.second, e.first, -1});
                }
            }
        }
    }
    
    ll ans = D[V];

    pq2 = priority_queue<ti, vector<ti>, greater<ti>>();
    fill(begin(D), end(D), INF);
    D[U] = 0;
    pq2.push({0, U, 0});
    while(!pq2.empty()){
        ti t = pq2.top(); pq2.pop();
        ll dist = get<0>(t), curr = get<1>(t), state = get<2>(t);
        if(dist != D[curr]) continue;
        if(curr == V) break;
        if(state != -1){
            for(auto e : pred[curr]){
                if(dist < D[e]){
                    pq2.push({D[e] = dist, e, 1});
                }
            }
            for(auto e : edge[curr]){
                if(dist + e.second < D[e.first]){
                    pq2.push({D[e.first] = dist + e.second, e.first, -state});
                }
            }
        }else{
            for(auto e : edge[curr]){
                if(dist + e.second < D[e.first]){
                    pq2.push({D[e.first] = dist + e.second, e.first, -1});
                }
            }
        }
    }
    cout << min(ans, D[V]) << "\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...