#include <bits/stdc++.h>
#include "crocodile.h"
using namespace std;
typedef long long ll;
mt19937 rng((long long) (new char));
const int N = 100000 + 7;
const int INF = (int) 1e9 + 7;
int n, m, k, best[N];
multiset<int> adj[N];
vector<pair<int, int>> g[N];
queue<int> q;
bool inside[N];
void push(int i, int value) {
if (!inside[i]) {
q.push(i);
inside[i] = 1;
}
for (auto &it : g[i]) {
adj[it.first].erase(adj[it.first].find(min(INF, best[i] + it.second)));
}
best[i] = value;
for (auto &it : g[i]) {
adj[it.first].insert(min(INF, best[i] + it.second));
}
}
int get_second(int a) {
if ((int) adj[a].size() < 2) {
return INF;
}
auto it = adj[a].begin();
it++;
return *it;
}
int travel_plan(int nn, int mm, int r[][2], int len[], int kk, int ch[]) {
n = nn;
m = mm;
k = kk;
for (int i = 0; i < n; i++) {
g[i].clear();
adj[i].clear();
best[i] = INF;
inside[i] = 0;
}
for (int i = 0; i < m; i++) {
g[r[i][0]].push_back({r[i][1], len[i]});
g[r[i][1]].push_back({r[i][0], len[i]});
adj[r[i][0]].insert(INF);
adj[r[i][1]].insert(INF);
}
shuffle(ch, ch + k, rng);
for (int i = 0; i < n; i++) {
shuffle(g[i].begin(), g[i].end(), rng);
}
for (int i = 0; i < k; i++) {
push(ch[i], 0);
}
int cnt = 0;
while (!q.empty()) {
int a = q.front();
inside[a] = 0;
q.pop();
for (auto &it : g[a]) {
int node = it.first, relax = get_second(node);
if (relax < best[node]) {
push(node, relax);
}
}
}
if (best[0] == INF) {
best[0] = -1;
}
return best[0];
}
Compilation message
crocodile.cpp: In function 'int travel_plan(int, int, int (*)[2], int*, int, int*)':
crocodile.cpp:63:7: warning: unused variable 'cnt' [-Wunused-variable]
63 | int cnt = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7500 KB |
Output is correct |
5 |
Correct |
5 ms |
7436 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7500 KB |
Output is correct |
8 |
Correct |
5 ms |
7500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7500 KB |
Output is correct |
5 |
Correct |
5 ms |
7436 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7500 KB |
Output is correct |
8 |
Correct |
5 ms |
7500 KB |
Output is correct |
9 |
Correct |
13 ms |
8012 KB |
Output is correct |
10 |
Correct |
5 ms |
7376 KB |
Output is correct |
11 |
Correct |
7 ms |
7628 KB |
Output is correct |
12 |
Correct |
32 ms |
8588 KB |
Output is correct |
13 |
Correct |
16 ms |
8652 KB |
Output is correct |
14 |
Correct |
6 ms |
7372 KB |
Output is correct |
15 |
Correct |
7 ms |
7500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7500 KB |
Output is correct |
5 |
Correct |
5 ms |
7436 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7500 KB |
Output is correct |
8 |
Correct |
5 ms |
7500 KB |
Output is correct |
9 |
Correct |
13 ms |
8012 KB |
Output is correct |
10 |
Correct |
5 ms |
7376 KB |
Output is correct |
11 |
Correct |
7 ms |
7628 KB |
Output is correct |
12 |
Correct |
32 ms |
8588 KB |
Output is correct |
13 |
Correct |
16 ms |
8652 KB |
Output is correct |
14 |
Correct |
6 ms |
7372 KB |
Output is correct |
15 |
Correct |
7 ms |
7500 KB |
Output is correct |
16 |
Execution timed out |
2090 ms |
126960 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |