제출 #1144916

#제출 시각아이디문제언어결과실행 시간메모리
1144916Robert_juniorCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
383 ms35588 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ins insert
#define all(x) x.begin(), x.end()
#define F first
#define S second
const int N  = 2e5 + 7;
int d[N], d1[N], d2[N], n, m, used[N], s, t, u, v, ans = 1e18, used1[N], used2[N];
int dp[N][2];
vector<int>ord;
vector<pair<int, int>>g[N], g1[N];
void dfs(int v){
    used1[v] = 1;
    for(auto [to, w] : g1[v]){
        if(used1[to]) continue;
        dfs(to);
    }
    ord.pb(v);
}
void dfs1(int v){
    used2[v] = 1;
    for(auto [to, w] : g[v]){
        if(d[to] + w == d[v]){
            g1[to].pb({v, 0});
            if(!used2[to]){
                dfs1(to);
            }
        }
    }
}
void dijkstra(){
    for(int i = 1; i <= n; i++){
        d[i] = 1e18;
        used[i] = 0;
        dp[i][0] = dp[i][1] = 1e18;
    }
    priority_queue<array<int, 3>>q;
    d[s] = 0;
    q.push({0, s, s});
    while(q.size()){
        int we = -q.top()[0], v = q.top()[1], pr = q.top()[2];
        q.pop();
        if(!used[v]){
            used[v] = 1;
            for(auto [to, w] : g[v]){
                if(d[to] >= d[v] + w){
                    d[to] = d[v] + w;
                    q.push({-d[to], to, v});
                }
            }
        }
    }
    dfs1(t);
}
void dijkstra1(int s){
    for(int i = 1; i <= n; i++){
        used[i] = 0;
        d1[i] = 1e18;
    }
    d1[s] = 0;
    priority_queue<pair<int, int>>q;
    q.push({0, s});
    while(q.size()){
        int v = q.top().second;
        q.pop();
        if(used[v]) continue;
        used[v] = 1;
        for(auto [to, w] : g[v]){
            if(d1[to] > d1[v] + w){
                d1[to] = d1[v] + w;
                q.push({-d1[to], to});
            }
        }
    }
    dfs1(t);
}
void dijkstra2(int s){
    for(int i = 1; i <= n; i++){
        used[i] = 0;
        d2[i] = 1e18;
    }
    d2[s] = 0;
    priority_queue<pair<int, int>>q;
    q.push({0, s});
    while(q.size()){
        int v = q.top().second;
        q.pop();
        if(used[v]) continue;
        used[v] = 1;
        for(auto [to, w] : g[v]){
            if(d2[to] > d2[v] + w){
                d2[to] = d2[v] + w;
                q.push({-d2[to], to});
            }
        }
    }
}
void solve(){
    cin>>n>>m>>s>>t>>u>>v;
    for(int i = 1; i <= m; i++){
        int a, b, w;
        cin>>a>>b>>w;
        g[a].pb({b, w});
        g[b].pb({a, w});
    }
    dijkstra1(u);
    dijkstra2(v);
    dijkstra();
    dfs(s);
    int ans = d1[v];
    for(auto a : ord){
        ans = min(ans, d1[a] + d2[a]);
        dp[a][0] = d2[a];
        dp[a][1] = d1[a];
        for(auto [to, w] : g1[a]){
            dp[a][0] = min(dp[a][0], dp[to][0]);
            dp[a][1] = min(dp[a][1], dp[to][1]);
            ans = min({ans, dp[a][0] + d1[a], dp[a][1] + d2[a]});
        }
    }
    cout<<ans<<'\n';
}
signed main(){
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...