#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
return l + abs((ll) rng()) % (r - l + 1);
}
const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 998244353;
const int mxn = 2e5 + 5, mxm = MASK(5) + 1, LOG = 18;
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
#include "crocodile.h"
struct Edge {
int v, w;
Edge(int _v, int _w) : v(_v), w(_w) {};
};
int n, m;
vector<Edge> g[mxn];
pair<int, int> a[4];
bool update(pair<int, int> &best, pair<int, int> &secondBest, pair<int, int> cur) {
a[0] = cur;
a[1] = best;
a[2] = secondBest;
sort(a, a + 3);
best = a[0];
int dist = secondBest.first;
secondBest = a[1].second == best.second ? a[2] : a[1];
return dist > secondBest.first;
}
int travel_plan(int N, int M, int R[][2], int L[], int K, int P[]) {
n = N; m = M;
forn(i, 0, m - 1) {
int u = R[i][0];
int v = R[i][1];
g[u].pb(Edge(v, L[i]));
g[v].pb(Edge(u, L[i]));
}
vector<pair<int, int>> best(n, mp(inf, inf)), secondBest;
secondBest = best;
forn(i, 0, K - 1) {
int node = P[i];
best[node] = secondBest[node] = mp(0, 0);
}
priority_queue<pair<ll, int>> pq;
forn(i, 0, n - 1) if (secondBest[i].first != inf) {
pq.push(mp(0, i));
}
while (!pq.empty()) {
pair<ll, int> top = pq.top(); pq.pop();
int u = top.second; ll dist = -top.fi;
if (secondBest[u].first != dist)
continue;
for (Edge i : g[u]) {
int v = i.v;
ll nxt = dist + i.w;
if (update(best[v], secondBest[v], mp(nxt, u))) {
pq.push(mp(-secondBest[v].first, v));
}
}
}
return secondBest[0].first;
}
//int main(void) {
//
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
//
// #define TASK "task"
// if (fopen(TASK".inp", "r")) {
// freopen(TASK".inp", "r", stdin);
// freopen(TASK".out", "w", stdout);
// }
//
// int n = 5, m = 4, k = 3;
// int r[][2] = {{0, 1}, {0, 2}, {3, 2}, {2, 4}};
// int l[] = {2, 3, 1, 4};
// int p[] = {1, 3, 4};
//
// cout << travel_plan(n, m, r, l, k, p);
//
// return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8800 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8800 KB |
Output is correct |
6 |
Correct |
1 ms |
8808 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8800 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8800 KB |
Output is correct |
6 |
Correct |
1 ms |
8808 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8800 KB |
Output is correct |
9 |
Incorrect |
3 ms |
9316 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8800 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8800 KB |
Output is correct |
6 |
Correct |
1 ms |
8808 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8800 KB |
Output is correct |
9 |
Incorrect |
3 ms |
9316 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |