This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
typedef long long ll;
typedef long double ldb;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vdb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef set<double> sdb;
typedef set<string> ss;
typedef set<char> sc;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ftb(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define ft(i, a, b) for (int i = a, _b = b; i < _b; ++i)
#define fgb(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define fg(i, a, b) for (int i = a, _b = b; i > _b; --i)
#define endl "\n"
void dijkstra(int node, vector<pll> adj[], vl& distance) {
    distance[node] = 0;
    priority_queue<pll, vector<pll>, greater<pll>> q;
    q.push({ 0,node });
    while (!q.empty()) {
        int node = q.top().second;
        ll d = q.top().first;
        q.pop();
        if (d > distance[node]) continue;
        for (pll it : adj[node]) {
            if (distance[it.first] > d + it.second) {
                distance[it.first] = d + it.second;
                q.push({ d + it.second,it.first });
            }
        }
    }
}
void dijkstra2(int node, vector<pll> adj[], vi parent[], int n) {
    vl distance(n + 1, 1e16);
    distance[node] = 0;
    priority_queue<pll, vector<pll>, greater<pll>> q;
    q.push({ 0,node });
    while (!q.empty()) {
        int node = q.top().second;
        ll d = q.top().first;
        q.pop();
        if (d > distance[node]) continue;
        for (pll it : adj[node]) {
            if (distance[it.first] > d + it.second) {
                distance[it.first] = d + it.second;
                q.push({ d + it.second,it.first });
                parent[it.first].clear();
                parent[it.first].push_back(node);
            }
            else if (distance[it.first] == d + it.second) {
                parent[it.first].push_back(node);
            }
        }
    }
}
void bfs(int n, int s, vi adj[], vl& disU, vl& disV, ll& ans) {
    vi cnt(n + 1, 0);
    ftb(i, 1, n) {
        for (int it : adj[i]) {
            cnt[it] += 1;
        }
    }
    queue<int> q;
    ftb(i, 1, n) {
        if (cnt[i] == 0 && i != s) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int node = q.front();
        q.pop();
        for (int it : adj[node]) {
            cnt[it] -= 1;
            if (cnt[it] == 0) {
                q.push(it);
            }
        }
    }
    vl mn(n + 1);
    ftb(i, 1, n) {
        mn[i] = disU[i];
    }
    q.push(s);
    while (!q.empty()) {
        int node = q.front();
        q.pop();
        ans = min(ans, mn[node] + disV[node]);
        for (int it : adj[node]) {
            cnt[it] -= 1;
            mn[it] = min(mn[it], mn[node]);
            if (cnt[it] == 0) {
                q.push(it);
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, m;
    cin >> n >> m;
    int s, t, u, v;
    cin >> s >> t >> u >> v;
    vector<pll> adj[n + 1];
    ft(i, 0, m) {
        ll x, y, z;
        cin >> x >> y >> z;
        adj[x].push_back({ y,z });
        adj[y].push_back({ x,z });
    }
    vl disU(n + 1, 1e16), disV(n + 1, 1e16);
    dijkstra(u, adj, disU);
    dijkstra(v, adj, disV);
    vi parent[n + 1];
    dijkstra2(s, adj, parent, n);
    vi cnt(n + 1, 0);
    ftb(i, 1, n) {
        for (int it : parent[i]) {
            cnt[it] += 1;
        }
    }
    queue<int> q;
    ftb(i, 1, n) {
        if (cnt[i] == 0 && i != t) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int node = q.front();
        q.pop();
        for (int it : parent[node]) {
            cnt[it] -= 1;
            if (cnt[it] == 0) {
                q.push(it);
            }
        }
        parent[node].clear();
    }
    vi adj2[n + 1];
    ftb(i, 1, n) {
        for (int it : parent[i]) {
            adj2[it].push_back(i);
        }
    }
    ll ans = disU[v];
    bfs(n, t, parent, disU, disV, ans);
    bfs(n, s, adj2, disU, disV, ans);
    if (ans == 1e16) ans = 0;
    cout << ans << endl;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |