This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
vector<vector<array<int, 2>>> adj(N);
for (int i = 0; i < N - 1; i++) {
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
auto bfs = [&](int s) {
vector<ll> dis(N);
vector<int> from(N, -1);
from[s] = s;
queue<int> q;
q.push(s);
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto [y, z] : adj[x]) {
if (from[y] == -1) {
from[y] = x;
dis[y] = dis[x] + z;
q.push(y);
}
}
}
return make_pair(dis, from);
};
auto [dis_x, from_x] = bfs(X);
auto [dis_y, from_y] = bfs(Y);
vector<ll> one(N), two(N);
for (int i = 0; i < N; i++) {
tie(one[i], two[i]) = minmax(dis_x[i], dis_y[i]);
}
int ans = 0; {
auto a(one);
sort(a.begin(), a.end());
ll sum = 0;
while (ans < N && sum + a[ans] <= K) sum += a[ans++];
}
vector<int> path{X};
while (path.back() != Y) path.push_back(from_y[path.back()]);
int M = path.size();
vector<int> on_path(N);
for (auto x : path) on_path[x] = 1;
vector<ll> dp(2 * N + 1, 1e18);
dp[0] = 0;
for (int i = 0; i < N; i++) {
if (!on_path[i]) {
for (int j = 2 * N; j >= 0; j--) {
if (j > 0) dp[j] = min(dp[j], dp[j - 1] + one[i]);
if (j > 1) dp[j] = min(dp[j], dp[j - 2] + two[i]);
}
} else {
for (int j = 2 * N; j >= 0; j--) {
dp[j] = min(j < 1 ? 1e18 : dp[j - 1] + one[i], j < 2 ? 1e18 : dp[j - 2] + two[i]);
}
}
}
for (int i = 0; i <= 2 * N; i++) {
if (dp[i] <= K) ans = max(ans, i);
}
return ans;
}
Compilation message (stderr)
closing.cpp: In function 'int max_score(int, int, int, ll, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:44:9: warning: unused variable 'M' [-Wunused-variable]
44 | int M = path.size();
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |