#include "crocodile.h"
#include <queue>
#include <vector>
using namespace std;
struct edge {
int x;
int y;
int w;
};
class solution {
const int kInf = 1e9 + 10;
const int n;
const int m;
const vector<edge> edges;
const int k;
const vector<int> end;
vector<vector<pair<int, int>>> g;
vector<int> dp_mn, dp_mn2;
void dijkstra() {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;
for (const int x : end) {
q.push(make_pair(0, x));
dp_mn[x] = dp_mn2[x] = 0;
}
while (not q.empty()) {
const auto [d, x] = q.top();
q.pop();
if (dp_mn2[x] < d) continue;
for (const auto &[y, w] : g[x]) {
int cur = d + w;
if (cur < dp_mn[y]) {
swap(dp_mn[y], cur);
}
if (cur < dp_mn2[y]) {
dp_mn2[y] = cur;
q.push(make_pair(dp_mn2[y], y));
}
}
}
}
public:
explicit solution(const int n, const vector<edge> &edges, const vector<int> &end)
: n(n), m(edges.size()), edges(edges), k(end.size()), end(end), g(n), dp_mn(n, kInf), dp_mn2(n, kInf) {
for (const auto &[x, y, w] : edges) {
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
}
int solve() {
dijkstra();
return dp_mn2[0];
}
};
int travel_plan(int N, int M, int R[][2], int L[], int K, int P[]) {
vector<edge> edges(M);
for (int i = 0; i < M; i++) {
edges[i].x = R[i][0];
edges[i].y = R[i][1];
edges[i].w = L[i];
}
vector<int> end(K);
for (int i = 0; i < K; i++) {
end[i] = P[i];
}
solution s(N, edges, end);
return s.solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
972 KB |
Output is correct |
13 |
Correct |
3 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
972 KB |
Output is correct |
13 |
Correct |
3 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
399 ms |
67996 KB |
Output is correct |
17 |
Correct |
76 ms |
17504 KB |
Output is correct |
18 |
Correct |
85 ms |
19020 KB |
Output is correct |
19 |
Correct |
479 ms |
73096 KB |
Output is correct |
20 |
Correct |
259 ms |
62396 KB |
Output is correct |
21 |
Correct |
36 ms |
8132 KB |
Output is correct |
22 |
Correct |
279 ms |
56004 KB |
Output is correct |