#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
// -------------------------------------------------- Main Code --------------------------------------------------
int sol(int n, int m, int (*edges)[2], int* l, int k, int *entry) {
vector<array<int, 2>> g[n];
for (int i = 0; i < m; i++) {
int a = edges[i][0], b = edges[i][1], c = l[i];
g[a].pb({b, c});
g[b].pb({a, c});
}
priority_queue<pii, vector<pii>, greater<pii>> q;
vii dis(n);
vi vis(n, 0);
for (int i = 0; i < k; i++) {
int temp = entry[i];
q.push({0, temp});
dis[temp].pb(0); dis[temp].pb(0);
vis[temp]++;
}
while (!q.empty()) {
int src = q.top().s, dist = q.top().f; q.pop();
if (vis[src] >= 2) continue;
vis[src]++;
if (vis[src] != 2) continue;
if (src == 0) {
return dist;
}
for (auto child : g[src]) {
int ch = child[0], wt = child[1];
if (dis[ch].size() == 2) {
sort (all(dis[ch]));
if (dist+wt < dis[ch][1]) {
dis[ch].pop_back();
dis[ch].pb(dist+wt);
q.push({dist+wt, ch});
}
}
else {
dis[ch].pb(dist+wt);
q.push({dist+wt, ch});
}
}
}
// not needed
cout << "BAD" << endl;
return -1;
}
int travel_plan(int n, int m, int (*r)[2], int* l, int k, int *p) {
return sol(n, m, r, l, k, p);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
2 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
2 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
2 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
450 ms |
57640 KB |
Output is correct |
17 |
Correct |
86 ms |
18740 KB |
Output is correct |
18 |
Correct |
100 ms |
18556 KB |
Output is correct |
19 |
Correct |
632 ms |
62976 KB |
Output is correct |
20 |
Correct |
298 ms |
47308 KB |
Output is correct |
21 |
Correct |
45 ms |
7548 KB |
Output is correct |
22 |
Correct |
314 ms |
45844 KB |
Output is correct |