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;
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
vector<vector<pair<int, int>>> graph(N);
for (int i = 0; i < (int)U.size(); ++i) {
graph[U[i]].emplace_back(V[i], W[i]);
graph[V[i]].emplace_back(U[i], W[i]);
}
auto precomp = [&](int src) {
vector<int> q = {src};
vector<long long> ret(N, -1);
ret[src] = 0;
for (int i = 0; i < N; ++i) {
int node = q[i];
for (auto [vec, cost] : graph[node]) {
if (ret[vec] == -1) {
ret[vec] = ret[node] + cost;
q.push_back(vec);
}
}
}
return ret;
};
vector<int> xy = {X, Y};
vector<vector<long long>> dist = {precomp(X), precomp(Y)};
vector<vector<int>> taken = {vector<int>(N, 0), vector<int>(N, 0)};
vector<vector<int>> seen = {vector<int>(N, 0), vector<int>(N, 0)};
priority_queue<tuple<long long, int, int>> pq;
auto push = [&](int z, int node) {
if (seen[z][node]) return;
seen[z][node] = true;
pq.emplace(-2 * dist[z][node], z, node);
if (seen[!z][node] && max(dist[0][node], dist[1][node]) <
2 * min(dist[0][node], dist[1][node])) {
pq.emplace(-(dist[z][node] + dist[!z][node]), 3, node);
}
};
push(0, xy[0]); push(1, xy[1]);
auto take = [&](int z, int node) {
if (taken[z][node]) return true;
long long d = dist[z][node];
if (taken[!z][node]) d = max(0LL, d - dist[!z][node]);
//cerr << "try take: " << node << " d=" << d << endl;
if (d > K) return false;
K -= d;
taken[z][node] = true;
for (auto [vec, _] : graph[node])
push(z, vec);
//cerr << "ok" << endl;
return true;
};
while (pq.size()) {
auto [_, z, node] = pq.top(); pq.pop();
if (z == 3) {
long long oldK = K;
int oldTake[2] = {taken[0][node], taken[1][node]};
if (!take(0, node) || !take(1, node)) {
K = oldK;
taken[0][node] = oldTake[0];
taken[1][node] = oldTake[1];
}
} else {
take(z, node);
}
}
/*
for (int z = 0; z < 2; ++z) {
for (int i = 0; i < N; ++i)
cerr << taken[z][i];
cerr << endl;
}
*/
int ans = 0;
for (int z = 0; z < 2; ++z)
for (int i = 0; i < N; ++i)
ans += taken[z][i];
return ans;
}
# | 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... |