Submission #898610

#TimeUsernameProblemLanguageResultExecution timeMemory
898610aykhnCommuter Pass (JOI18_commuter_pass)C++17
31 / 100
242 ms28620 KiB
#include <bits/stdc++.h>

// author: aykhn

using namespace std;
typedef long long ll;

#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define mpr make_pair
#define eb emplace_back
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define ins insert
#define int ll
#define inf 0x3F3F3F3F
#define infll 0x3F3F3F3F3F3F3F3FLL
#define bpc __builtin_popcount

const int MXN = 1e5 + 5;

int dist[2][MXN];
int d1[MXN];
array<array<int, 2>, 4> d[MXN];
vector<array<int, 2>> adj[MXN];

array<int, 2> comp(array<int, 2> a, array<int, 2> b, int t)
{
    if (!t) return (a[0] + a[1] == b[0] + b[1] ? min(a, b) : (a[0] + a[1] < b[0] + b[1] ? a : b));
    else if (t == 1) 
    {
        swap(a[0], a[1]), swap(b[0], b[1]);
        if (a > b) swap(a, b);
        swap(a[0], b[0]), swap(b[0], b[1]);
        return (a[0] + a[1] == b[0] + b[1] ? a : (a[0] + a[1] < b[0] + b[1] ? a : b));
    }
    else if (t == 2) return min(a, b);
    else
    {
        swap(a[0], a[1]), swap(b[0], b[1]);
        if (a > b) swap(a, b);
        swap(a[0], a[1]);
        return a;
    }
}

void dijk(int s, int id)
{
    for (int i = 0; i < MXN; i++) dist[id][i] = infll;
    priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
    dist[id][s] = 0;
    pq.push({0, s});
    while (!pq.empty())
    {
        array<int, 2> f = pq.top();
        pq.pop();
        if (f[0] > dist[id][f[1]]) continue;
        for (const array<int, 2> &v : adj[f[1]])
        {
            if (dist[id][v[0]] > f[0] + v[1])
            {
                dist[id][v[0]] = f[0] + v[1];
                pq.push({dist[id][v[0]], v[0]});
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n, m, s, t, u, v;
    cin >> n >> m >> s >> t >> u >> v;
    while (m--)
    {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }
    dijk(u, 0);
    dijk(v, 1);
    for (int i = 0; i < MXN; i++) 
    {
        for (int j = 0; j < 4; j++) d[i][j] = {infll, infll};
        d1[i] = infll;
    }
    priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
    d1[s] = 0;
    d[s][0] = d[s][1] = d[s][2] = d[s][3] = {dist[0][s], dist[1][s]};
    pq.push({0, s});
    while (!pq.empty())
    {
        int f = pq.top()[1];
        int w = pq.top()[0];
        for (int i = 0; i < 4; i++) d[f][i][0] = min(d[f][i][0], dist[0][f]), d[f][i][1] = min(d[f][i][1], dist[1][f]);
        pq.pop();
        if (w > d1[f]) continue;
        for (const array<int, 2> &v : adj[f])
        {
            if (d1[v[0]] > w + v[1])
            {
                d1[v[0]] = w + v[1];
                d[v[0]] = d[f];
                pq.push({d1[v[0]], v[0]});
            }
            else if (d1[v[0]] == w + v[1])
            {
                for (int i = 0; i < 4; i++) d[v[0]][i] = comp(d[f][3], comp(d[f][2], comp(d[f][1], comp(d[v[0]][i], d[f][0], 0), 0), 0), 0);
                // pq.push({d1[v[0]], v[0]});
            }
        }
    }
    cout << min(d[t][0][0] + d[t][0][1], dist[0][v]) << '\n';
}

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