#include "crocodile.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#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];
pl 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 };
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<pl, int>> pq;
for (int i = 0; i < K; ++i)
{
cost[P[i]] = { 0, 0 };
// pq.push({ {0, 0}, P[i] });
}
dfs(0, -1);
return cost[0].s2;
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;
};
while (!pq.empty())
{
auto [C, node] = pq.top(); pq.pop();
C.f1 = -C.f1; C.s2 = -C.s2;
if (C != cost[node])
continue;
for (auto &[to, w] : adj[node])
{
pl nxt = merge(cost[to], C.s2 + w);
if (nxt < cost[to])
{
cost[to] = nxt;
pq.push({{-nxt.f1, -nxt.s2}, to});
}
}
}
return (int)cost[0].s2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2672 KB |
Output is correct |
9 |
Runtime error |
144 ms |
262144 KB |
Execution killed with signal 9 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2672 KB |
Output is correct |
9 |
Runtime error |
144 ms |
262144 KB |
Execution killed with signal 9 |
10 |
Halted |
0 ms |
0 KB |
- |