#include "crocodile.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef vector<pii> vii;
const int MAXN = 100100;
int n, m;
vii adj[MAXN];
vi exits;
ll d1[MAXN], d2[MAXN];
bool vis[MAXN];
void addD (int v, ll d) {
if (d1[v] == -1) {
d1[v] = d;
return;
}
if (d2[v] == -1) {
if (d > d1[v])
d2[v] = d;
else {
d2[v] = d1[v];
d1[v] = d;
}
return;
}
if (d >= d2[v]) return;
d2[v] = d;
if (d2[v] < d1[v]) swap(d1[v], d2[v]);
}
void dijkstra () {
FOR(i, 0, n-1) d1[i] = d2[i] = -1;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
for (int v : exits) {
d1[v] = d2[v] = 0;
q.push({d2[v], v});
}
while (!q.empty()) {
int v = q.top().s;
q.pop();
if (!vis[v]) {
// cout << " v = " << v << endl;
vis[v] = true;
for (auto e : adj[v]) {
int u = e.f;
ll w = e.s;
ll was = d2[u];
addD(u, d2[v] + w);
//cout << " u = " << u << " was = " << was << " now =" << d2[u] << " d2+w = " << d2[v]+w << endl;
if (was != d2[u]) {
q.push({d2[u], u});
}
}
}
}
}
int travel_plan(int N, int M, int R[][2], int L[], int K, int P[])
{
n = N;
m = M;
FOR(i, 0, m-1) {
int u = R[i][0];
int v = R[i][1];
int w = L[i];
adj[u].pb({v, w});
adj[v].pb({u, w});
}
FOR(i, 0, K-1)
exits.pb(P[i]);
dijkstra();
return d2[0];
}
/*
input 1
5 4 3
0 1 2
0 2 3
3 2 1
2 4 4
1 3 4
7
input 2
5 7 2
0 2 4
0 3 3
3 2 2
2 1 10
0 1 100
0 4 7
3 4 9
1 3
14
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
3 ms |
2796 KB |
Output is correct |
6 |
Correct |
3 ms |
2796 KB |
Output is correct |
7 |
Correct |
4 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
3 ms |
2796 KB |
Output is correct |
6 |
Correct |
3 ms |
2796 KB |
Output is correct |
7 |
Correct |
4 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2944 KB |
Output is correct |
9 |
Correct |
6 ms |
3052 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
6 ms |
3180 KB |
Output is correct |
13 |
Correct |
5 ms |
3180 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
4 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
3 ms |
2796 KB |
Output is correct |
6 |
Correct |
3 ms |
2796 KB |
Output is correct |
7 |
Correct |
4 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2944 KB |
Output is correct |
9 |
Correct |
6 ms |
3052 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
6 ms |
3180 KB |
Output is correct |
13 |
Correct |
5 ms |
3180 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
4 ms |
2796 KB |
Output is correct |
16 |
Correct |
514 ms |
44796 KB |
Output is correct |
17 |
Correct |
78 ms |
13164 KB |
Output is correct |
18 |
Correct |
104 ms |
14572 KB |
Output is correct |
19 |
Correct |
662 ms |
50020 KB |
Output is correct |
20 |
Correct |
335 ms |
38636 KB |
Output is correct |
21 |
Correct |
41 ms |
7532 KB |
Output is correct |
22 |
Correct |
342 ms |
34156 KB |
Output is correct |