Submission #483663

#TimeUsernameProblemLanguageResultExecution timeMemory
483663VictorCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
1035 ms51524 KiB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const int INF = 1'000'000'007;

vpll graph[100001];
int n, m, s, t, a, b;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    cin >> n >> m >> s >> t >> a >> b;
    --s;
    --t;
    --a;
    --b;
    rep(i, 0, m) {
        ll u, v, w;
        cin >> u >> v >> w;
        graph[--u].emplace_back(--v, w);
        graph[v].emplace_back(u, w);
    }

    ll dist[100001];
    memset(dist, 63, sizeof(dist));

    dist[s] = 0;
    set<pll> pq;
    pq.emplace(0, s);

    while (!pq.empty()) {
        ll u, d;
        tie(d, u) = *pq.begin();
        pq.erase(pq.begin());

        trav(edge, graph[u]) {
            ll v, w;
            tie(v, w) = edge;
            if (d + w >= dist[v]) continue;

            pq.erase({dist[v], v});
            dist[v] = d + w;
            pq.emplace(d + w, v);
        }
    }

    set<pll> marked;
    bitset<100001> vis;
    queue<ll> q;
    q.push(t);
    vis[t] = 1;

    while (!q.empty()) {
        ll u = q.front();
        q.pop();
        trav(edge, graph[u]) {
            ll v, w;
            tie(v, w) = edge;
            if (dist[u] - w == dist[v]) {
                marked.emplace(u, v);
                marked.emplace(v, u);

                if (!vis[v]) {
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }

    ll cost[100001][4];
    memset(cost, 63, sizeof(cost));
    cost[a][0] = 0;
    pq.emplace(0, a);

    while (!pq.empty()) {
        ll u, d, state;
        tie(d, u) = *pq.begin();
        pq.erase(pq.begin());
        state = u >> 30;
        u &= (1 << 30) - 1;

        // cout<<"u = "<<u<<" d = "<<d<<" state = "<<state<<endl;

        trav(edge, graph[u]) {
            ll v, w;
            tie(v, w) = edge;

            if (state != 3 && marked.count(pll(u, v))) {
                ll nstate = (dist[u] < dist[v]) + 1;
                if ((!state || state == nstate) && d < cost[v][nstate]) {
                    pq.erase({cost[v][nstate], v | (nstate << 30)});
                    cost[v][nstate] = d;
                    pq.emplace(d, v | (nstate << 30));
                }
            }

            ll nstate = state ? 3 : 0;
            if (d + w < cost[v][nstate]) {
                pq.erase({cost[v][nstate], v | (nstate << 30)});
                cost[v][nstate] = d + w;
                pq.emplace(d + w, v | (nstate << 30));
            }
        }
    }
    ll ans = 1e15;
    rep(i, 0, 4) ans = min(ans, cost[b][i]);
    cout << ans << endl;
    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...