#include <bits/stdc++.h>
#include "crocodile.h"
using namespace std;
#define ff first
#define ss second
#define pii pair<int, int>
using ll = long long;
vector<bool> vis;
vector<ll> v, dis;
vector<vector<pii>> E;
void dfs(int x, int p = -1) {
vis[x] = true;
if (!dis[x]) return v[x] = 0, void();
for (auto [i, w] : E[x]) {
if (!vis[i]) dfs(i, x);
}
vector<ll> u, t;
for (auto [i, w] : E[x]) {
if (i != p && v[i] != LLONG_MAX) (dis[x] != w + dis[i] ? u : t).push_back(v[i] + w);
}
if (1 < (int)t.size()) u.insert(u.end(), t.begin(), t.end());
if (!u.empty()) v[x] = *min_element(u.begin(), u.end());
}
int travel_plan(int n, int M, int R[][2], int L[], int K, int P[]) {
E.assign(n, {});
for (int i = 0; i < M; i++) {
E[R[i][0]].push_back({R[i][1], L[i]});
E[R[i][1]].push_back({R[i][0], L[i]});
}
dis.assign(n, LLONG_MAX);
priority_queue<pair<ll, int>> q;
for (int i = 0; i < K; i++) {
dis[P[i]] = 0;
q.push({0, P[i]});
}
while (!q.empty()) {
auto [d, x] = q.top(); d = -d; q.pop();
if (d != dis[x]) continue;
for (auto [i, w] : E[x]) {
if (d + w < dis[i]) {
dis[i] = d + w;
q.push({-dis[i], i});
}
}
}
vis.assign(n, false);
v.assign(n, LLONG_MAX);
dfs(0);
return (v[0] == LLONG_MAX ? -1 : v[0]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |