Submission #1295702

#TimeUsernameProblemLanguageResultExecution timeMemory
1295702ionut27Commuter Pass (JOI18_commuter_pass)C++20
0 / 100
1526 ms327680 KiB
#include "bits/stdc++.h"
#include <type_traits>
using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

// ============ Macros starts here ============
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif // DEBUG
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream&>::type operator<<(Ostream& os, const Cont& v) {
    os << "{";
    for (auto& x : v) { os << x << ", "; }
    return os << "}";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os, const pair<Ts...>& p) {
    return os << "{" << p.first << ", " << p.second << "}";
}

#define readFast                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#ifdef LOCAL
#define read() ifstream fin("date.in.txt")
#else
#define read() readFast
#endif // LOCAL
// ============ Macros ends here ============

#define fin cin
#define ll long long
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define output(x) (((int)(x) && cout << "YES\n") || cout << "NO\n")
#define LSB(x) (x & (-x))
#define test cout << "WORKS\n";

const int N = 1e5 + 15;
const int MOD = 1e9 + 7;

// vector<pair<int, int>> graf[N];
unordered_map<int, unordered_map<int, int>> graf;
vector<int> from[N];
int n, m, S, T, U, V;

vector<ll> djk(vector<int> start) {
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> que;
    vector<ll> dist(n, LLONG_MAX);

    for (int node : start) {
        que.push({ 0, node });
        dist[node] = 0;
    }
    while (!que.empty()) {
        ll cost = que.top().first;
        int node = que.top().second;
        que.pop();

        if (cost != dist[node]) {
            continue;
        }

        for (auto [to, w] : graf[node]) {
            if (dist[to] > cost + w) {
                dist[to] = cost + w;
                que.push({ dist[to], to });
            }
        }
    }
    return dist;
}


int main() {
    read();
    fin >> n >> m;
    fin >> S >> T;
    fin >> U >> V;
    --S, --T, --U, --V;

    for (int i = 0; i < m; ++i) {
        int a, b, c;
        fin >> a >> b >> c;
        --a, --b;
        if (graf.find(a) != graf.end() && graf[a].find(b) != graf[a].end()) {
            graf[a][b] = min(graf[a][b], c);
            graf[b][a] = graf[a][b];
        } else {
            graf[a][b] = c;
            graf[b][a] = c;
        }
    }
    vector<ll> dU = djk({ U });
    vector<ll> dV = djk({ V });
    dbg(dU);
    dbg(dV);

    priority_queue<vector<ll>, vector<vector<ll>>, greater<vector<ll>>> que;
    vector<ll> dist(n, LLONG_MAX);
    vector<ll> distForUV(n, LLONG_MAX);

    que.push({ 0, dU[S] + dV[S], S, dU[S], dV[S] });
    dist[S] = 0;
    distForUV[S] = dU[S] + dV[S];
    ll ans = dU[V];


    while (!que.empty()) {
        ll cost = que.top()[0];
        ll minSum = que.top()[1];
        int node = que.top()[2];
        ll minCostU = que.top()[3];
        ll minCostV = que.top()[4];
        dbg(que.top());
        que.pop();

        if (node == T) {
            ans = min(ans, minSum);
        }

        if (dist[node] != cost) {
            continue;
        }

        for (auto [to, w] : graf[node]) {
            ll minSumUV = min(minCostU, dU[to]) + min(minCostV, dV[to]);

            if (dist[to] > dist[node] + w) {
                dist[to] = dist[node] + w;
                distForUV[to] = minSumUV;

                que.push({ dist[to], minSumUV, to, min(minCostU, dU[to]), min(minCostV, dV[to]) });
            } else if (dist[to] == dist[node] + w && distForUV[to] >= minSumUV) {
                distForUV[to] = minSumUV;

                que.push({ dist[to], minSumUV, to, min(minCostU, dU[to]), min(minCostV, dV[to]) });
            }
        }
    }
    cout << ans << '\n';

    return 0;
} /*stuff you should look for !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   * test the solution with the given example
   * int overflow, array bounds, matrix bounds
   * special cases (n=1?)
   * do smth instead of nothing and stay organized
   * WRITE STUFF DOWN
   * DON'T GET STUCK ON ONE APPROACH
~Benq~*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...