제출 #1150806

#제출 시각아이디문제언어결과실행 시간메모리
1150806DON_FCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
141 ms17756 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define L(i, b, e) for (int i = b; i < e; ++i)
#define R(i, b, e) for (int i = b; i >= e; --i)
#define pb emplace_back
#define vi vector<int>
#define sz(x) ((int) x.size())
const int N = 1e5 + 7, Mx = 1e9 + 9;
int n, m;
int s, t, u, v;
vector<pair<int, int>> g[N];
ll dis1[N], dis2[N];

void dijkstra(int x, ll d[]){
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
    d[x] = 0;
    pq.push({0, x});
    while (!pq.empty()){
        auto [w, y] = pq.top();
        pq.pop();
        if (d[y] != w)continue;
        for (auto &i : g[y]){
            if (w + i.second < d[i.first]){
                d[i.first] = w + i.second;
                pq.push({d[i.first], i.first});
            }
        }
    }
}

ll dis3[N];
ll mn1[N], mn2[N];
ll res1[N], res2[N];
bool vis[N];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m >> s >> t >> u >> v;
    s--;
    t--;
    u--;
    v--;
    L(i, 0, m){
        int a, b, w;
        cin >> a >> b >> w;
        a--;
        b--;
        g[a].pb(b, w);
        g[b].pb(a, w);
    }
    L(i, 0, n)dis1[i] = dis2[i] = dis3[i] = 1LL * Mx * Mx;
    L(i, 0, n)res1[i] = res2[i] = 1LL * Mx * Mx;
    dijkstra(u, dis1);
    dijkstra(v, dis2);
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
    dis3[s] = 0;
    pq.push({0, s});
    while (!pq.empty()){
        auto [d, y] = pq.top();
        pq.pop();
        if (vis[y])continue;
        vis[y] = true;
        mn1[y] = dis1[y];
        mn2[y] = dis2[y];
        for (auto &i : g[y]){
            if (d + i.second < dis3[i.first]){
                dis3[i.first] = d + i.second;
                pq.push({dis3[i.first], i.first});
            }else if (i.second + dis3[i.first] == d){
                mn1[y] = min(mn1[y], mn1[i.first]);
                res1[y] = min(res1[y], res1[i.first]);
                mn2[y] = min(mn2[y], mn2[i.first]);
                res2[y] = min(res2[y], res2[i.first]);
            }
        }
        res1[y] = min(res1[y], mn1[y] + dis2[y]);
        res2[y] = min(res2[y], mn2[y] + dis1[y]);
    }
    cout << min({dis1[v], res1[t], res2[t]});
}



#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...