#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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
3028 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
5 ms |
3284 KB |
Output is correct |
13 |
Correct |
5 ms |
3188 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
3028 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
5 ms |
3284 KB |
Output is correct |
13 |
Correct |
5 ms |
3188 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
510 ms |
67376 KB |
Output is correct |
17 |
Correct |
77 ms |
16012 KB |
Output is correct |
18 |
Correct |
107 ms |
17628 KB |
Output is correct |
19 |
Correct |
953 ms |
102456 KB |
Output is correct |
20 |
Correct |
255 ms |
49872 KB |
Output is correct |
21 |
Correct |
39 ms |
8512 KB |
Output is correct |
22 |
Correct |
293 ms |
46924 KB |
Output is correct |