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 "crocodile.h"  
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f1 first
#define s2 second
using ll = long long;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
const int MAX = 1e5 + 69;
const ll inf = (ll) 1e18 + 69;
vector<ii> adj[MAX];
array<ll, 4> cost[MAX];
// inline void dfs(int node, int par)
// {
//     if (cost[node].f1 == 0)
//         return;
//     const auto merge = [](pl x, ll y) -> pl
//     {
//         if (x.f1 > y) swap(x.f1, y);
//         if (x.s2 > y) swap(x.s2, y);
//         if (x.f1 > x.s2) swap(x.f1, x.s2);
//         return x;
//     };
//     for (auto &[to, w] : adj[node]) if (to != par)
//     {
//         dfs(to, node);
//         cost[node] = merge(cost[node], w + cost[to].s2);
//     }
// }
int travel_plan(int N, int M, int R[][2], int L[], int K, int P[])
{
    for (int i = 0; i < N; ++i)
        cost[i] = { inf, inf, inf, inf };
    for (int i = 0; i < M; ++i)
    {
        adj[ R[i][0] ].pb({ R[i][1], L[i] });
        adj[ R[i][1] ].pb({ R[i][0], L[i] });
    }
    priority_queue<pair<array<ll, 4>, int>> pq;
    for (int i = 0; i < K; ++i)
    {
        cost[P[i]] = { 0, -1, 0, -1 };
        pq.push({cost[P[i]], P[i]});
    }
    // dfs(0, -1);
    // return cost[0].s2;
    const auto merge = [](array<ll, 4> x, ll c, ll p) -> array<ll, 4>
    {
        if (x[1] == p)
        {
            if (x[0] > c)
                swap(x[0], c);
        }
        else if (x[3] == p)
        {
            if (x[2] > c)
                swap(x[2], c);
        }
        else
        {
            if (x[0] > c)
                swap(x[0], c), swap(x[1], p);
            if (x[2] > c)
                swap(x[2], c), swap(x[3], p);
        }
        if (x[0] > x[2])
            swap(x[0], x[2]), swap(x[1], x[3]);
        return {x[0], x[1], x[2], x[3]};
    };
    while (!pq.empty())
    {
        auto [C, node] = pq.top(); pq.pop();
        C[0] = -C[0]; C[2] = -C[2];
        // cerr << node << " -> ";
        // for (ll x : C)
        //     cerr << x << ", ";
        // cerr << '\n';
        if (C != cost[node])
            continue;
        for (auto &[to, w] : adj[node])
        {
            array<ll, 4> nxt = merge(cost[to], C[2] + w, node);
            if (nxt < cost[to])
            {
                cost[to] = nxt;
                pq.push({{-nxt[0], nxt[1], -nxt[2], nxt[3]}, to});
            }
        }
    }
    return (int)cost[0][2];
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |