제출 #575984

#제출 시각아이디문제언어결과실행 시간메모리
575984SmolBrainCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
529 ms29404 KiB
// Om Namah Shivaya
// International Master in 40 days

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

bool iseven(ll a) {
    if (!(a & 1)) return true;
    else return false;
}

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#define debug(x) cout << #x << " = "; print(x); cout << endl

void print(int i) {
    cout << i;
}

void print(ll i) {
    cout << i;
}

void print(string i) {
    cout << i;
}

void print(char i) {
    cout << i;
}

void print(double i) {
    cout << i;
}

void print(ld i) {
    cout << i;
}

template<typename T, typename V>
void print(pair<T, V> v) {
    cout << "{";
    print(v.ff);
    cout << ",";
    print(v.ss);
    cout << "}";
}

template<typename T>
void print(vector<T> v) {
    cout << "[ ";

    for (auto i : v) {
        print(i);
        cout << " ";
    }

    cout << "]";
}

template<typename T>
void print(set<T> v) {
    cout << "[ ";

    for (auto i : v) {
        print(i);
        cout << " ";
    }

    cout << "]";
}

template<typename T>
void print(multiset<T> v) {
    cout << "[ ";

    for (auto i : v) {
        print(i);
        cout << " ";
    }

    cout << "]";
}

template<typename T>
void print(Tree<T> v) {
    cout << "[ ";

    for (auto i : v) {
        print(i);
        cout << " ";
    }

    cout << "]";
}

template<typename T, typename V>
void print(map<T, V> v) {
    cout << "{ ";
    for (auto p : v) {
        print(p);
        cout << " ";
    }

    cout << "}";
}

const int MOD = 1e9 + 7;
const int maxn = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<pll> adj[maxn];
vector<ll> adj2[maxn];
vector<bool> vis(maxn);
vector<ll> topo;

void dfs(ll node) {
    vis[node] = 1;

    trav(child, adj2[node]) {
        if (vis[child]) conts;
        dfs(child);
    }

    topo.pb(node);
}

void solve(int test_case)
{
    // https://usaco.guide/problems/joi-2018commuter-pass/solution
    ll n, m; cin >> n >> m;
    ll s, t; cin >> s >> t;
    ll x, y; cin >> x >> y;

    rep1(i, m) {
        ll u, v, w; cin >> u >> v >> w;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }

    auto dijkstra = [&](ll src, vector<ll> &dis) {
        priority_queue< pll, vector<pll>, greater<pll> > pq;
        fill(all(vis), 0);

        pq.push({0, src});
        dis[src] = 0;

        while (!pq.empty()) {
            auto [cost, node] = pq.top();
            pq.pop();

            if (vis[node]) conts;

            vis[node] = 1;

            for (auto [child, w] : adj[node]) {
                ll cost2 = cost + w;
                if (cost2 < dis[child]) {
                    dis[child] = cost2;
                    pq.push({cost2, child});
                }
            }
        }
    };

    auto get = [&]() {
        rep1(i, n) {
            adj2[i].clear();
            vis[i] = 0;
        }

        topo.clear();

        priority_queue< pll, vector<pll>, greater<pll> > pq;
        vector<ll> dis_s(n + 5, inf2), dis_t(n + 5, inf2), dis_x(n + 5, inf2), dis_y(n + 5, inf2);

        dijkstra(s, dis_s);
        dijkstra(t, dis_t);
        dijkstra(x, dis_x);
        dijkstra(y, dis_y);

        rep1(u, n) {
            for (auto [v, w] : adj[u]) {
                if (dis_s[u] + w == dis_s[v]) {
                    if (dis_s[u] + w + dis_t[v] == dis_s[t]) {
                        adj2[u].pb(v);
                    }
                }
            }
        }

        rep1(i, n) vis[i] = 0;

        rep1(i, n) {
            if (vis[i]) conts;
            dfs(i);
        }

        reverse(all(topo));

        // dp_x[i] = min(dis_x[j]) for all j such that edge j->i exists in dag
        // dp_y[i] = min(dis_y[j]) for all j such that edge i->j exists in dag

        vector<ll> dp_x(n + 5, inf2), dp_y(n + 5, inf2);

        trav(u, topo) {
            amin(dp_x[u], dis_x[u]);

            trav(v, adj2[u]) {
                amin(dp_x[v], dp_x[u]);
            }
        }

        reverse(all(topo));

        trav(u, topo) {
            amin(dp_y[u], dis_y[u]);

            trav(v, adj2[u]) {
                amin(dp_y[u], dp_y[v]);
            }
        }

        ll ans = inf2;
        rep1(u, n) {
            ll cost = dp_x[u] + dp_y[u];
            amin(ans, cost);
        }

        return ans;
    };

    ll ans = get();
    swap(s, t);
    amin(ans, get());

    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...