QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#523944 | #5102. Dungeon Crawler | Andycipation | WA | 0ms | 3612kb | C++20 | 2.8kb | 2024-08-19 01:06:45 | 2024-08-19 01:06:45 |
Judging History
answer
/*
* author: ADMathNoob
* created: 08/18/24 10:47:30
* problem: https://qoj.ac/problem/5102
*/
/*
Comments about problem:
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2005;
const int H = 13;
int n, q;
vector<pair<int, int>> g[N];
int pv[N];
int pre[N], post[N];
vector<int> order;
long long d[N];
long long maxD[N];
int pr[N][H];
void Dfs(int v, int p) {
pv[v] = p;
maxD[v] = d[v];
pre[v] = order.size();
order.push_back(v);
for (auto [u, w] : g[v]) {
if (u == p) continue;
d[u] = d[v] + w;
Dfs(u, v);
maxD[v] = max(maxD[v], maxD[u]);
}
post[v] = order.size() - 1;
}
void DfsFrom(int v) {
d[v] = 0;
order.clear();
Dfs(v, -1);
for (int i = 0; i < n; i++) {
fill(pr[i], pr[i] + H, -1);
}
for (int i = 0; i < n; i++) {
pr[i][0] = pv[i];
}
for (int j = 1; j < H; j++) {
for (int i = 0; i < n; i++) {
pr[i][j] = (pr[i][j - 1] == -1 ? -1 : pr[pr[i][j - 1]][j - 1]);
}
}
}
bool Anc(int x, int y) {
return pre[x] <= pre[y] && post[y] <= post[x];
}
int Lca(int x, int y) {
if (Anc(x, y)) return x;
if (Anc(y, x)) return y;
for (int j = H - 1; j >= 0; j--) {
if (pr[x][j] != -1 && !Anc(pr[x][j], y)) {
x = pr[x][j];
}
}
return pv[x];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
long long total = 0;
for (int i = 0; i < n - 1; i++) {
int x, y, w;
cin >> x >> y >> w;
--x; --y;
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
total += w;
}
vector<int> st(q), v1(q), v2(q);
vector<vector<int>> qs(n);
for (int i = 0; i < q; i++) {
cin >> st[i] >> v1[i] >> v2[i];
--st[i]; --v1[i]; --v2[i];
qs[st[i]].push_back(i);
}
vector<long long> ret(q);
for (int r = 0; r < n; r++) {
DfsFrom(r);
vector<long long> pref(n + 1);
for (int i = 0; i < n; i++) {
pref[i + 1] = max(pref[i], d[order[i]]);
}
vector<long long> suf(n + 1);
for (int i = n - 1; i >= 0; i--) {
suf[i] = max(suf[i + 1], d[order[i]]);
}
auto Solve = [&](int a, int b) {
if (Anc(a, b)) {
return 2 * total - maxD[r];
}
if (Anc(b, a)) {
return -1LL;
}
int L = Lca(a, b);
// if not ending in a, just take maxD
long long res = 2 * total - max(pref[pre[a]], suf[post[a] + 1]);
// if ending in a, need to take (L to a) path 3 times
res = min(res, 2 * total - maxD[a] + 2 * (d[a] - d[L]));
return res;
};
for (int qid : qs[r]) {
ret[qid] = Solve(v1[qid], v2[qid]);
}
}
for (int r : ret) {
if (r == -1) {
cout << "impossible\n";
} else {
cout << r << '\n';
}
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3612kb
input:
22 5 1 2 7 2 3 5 3 4 8 3 6 6 3 7 3 2 5 6 5 8 2 8 9 11 2 10 16 1 11 12 11 12 4 11 13 9 1 14 25 14 15 4 15 16 5 15 17 6 15 18 1 15 19 8 14 20 7 20 21 9 20 22 17 1 19 9 1 9 19 1 8 9 1 9 8 2 22 11
output:
293 293 293 impossible 294
result:
wrong answer 1st lines differ - expected: '316', found: '293'