제출 #1000532

#제출 시각아이디문제언어결과실행 시간메모리
1000532PanndaCommuter Pass (JOI18_commuter_pass)C++17
31 / 100
403 ms58012 KiB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
vector<ll> dijkstra(int s, vector<vector<pair<ll, ll>>> &adj){
    int n = adj.size();
    vector<ll> dist(n+1, INF);
    dist[s] = 0;
    vector<bool> vis(n+1, false);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push({0, s});
    while(!pq.empty()){
        ll d_u = pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if(vis[u]){
            continue;
        }
        vis[u] = true;
        for(auto it : adj[u]){
            int v = it.first;
            ll w = it.second;
            if(vis[v]) continue;
            dist[v] = min(dist[v], d_u+w);
            pq.push({dist[v], v});
        }
    }
    return dist;
}
void solve(){
    int n, m;
    cin >> n >> m;
    int S, T, U, V;
    cin >> S >> T;
    cin >> U >> V;
    vector<vector<pair<ll, ll>>> adj(n+1), adj1, adj2;
    for(int i = 1; i <= m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    adj1 = adj;
    adj2 = adj;
    auto edges_that_may_belong_to_a_shortest_path_from_s_to_t = [&]() -> vector<array<int, 2>> {
        vector<array<int, 2>> ret;
        vector<ll> dist = dijkstra(S, adj);
        vector<vector<int>> rev_dag(n+1);
        for (int u = 1; u <= n; u++) {
            for (auto [v, w] : adj[u]) {
                if (dist[u] + w == dist[v]) {
                    rev_dag[v].push_back(u);
                }
            }
        }
        vector<bool> vis(n+1, false);
        queue<int> que;
        vis[T] = true;
        que.push(T);
        while (!que.empty()) {
            int u = que.front();
            que.pop();
            for (int v : rev_dag[u]) {
                ret.push_back({v, u});
                if (!vis[v]) {
                    vis[v] = true;
                    que.push(v);
                }
            }
        }
        return ret;
    }();
    vector<ll> ds = dijkstra(S, adj), dt = dijkstra(T, adj);
    for (auto [u, v] : edges_that_may_belong_to_a_shortest_path_from_s_to_t) adj1[u].push_back({v, 0});
    for (auto [u, v] : edges_that_may_belong_to_a_shortest_path_from_s_to_t) adj2[v].push_back({u, 0});
    vector<ll> du1 = dijkstra(U, adj1), du2 = dijkstra(U, adj2), dv = dijkstra(V, adj);
    ll ans = INF;
    for(int i = 1; i <= n; i++){
        ans = min(ans, du1[i]+dv[i]);
        ans = min(ans, du2[i]+dv[i]);
    }
    cout << ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("main.inp", "r", stdin);
    //freopen("main.out", "w", stdout);
    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...