QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#467872 | #5102. Dungeon Crawler | karuna | WA | 1ms | 3668kb | C++20 | 1.6kb | 2024-07-08 18:02:07 | 2024-07-08 18:02:08 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int SZ = 2020;
int n, q;
vector<pii> g[SZ];
int par[SZ], col[SZ];
ll dep[SZ], mx[SZ];
void dfs(int p, int v) {
mx[v] = dep[v];
for (auto [w, x] : g[v]) if (p != x) {
dep[x] = dep[v] + w;
par[x] = v;
dfs(v, x);
mx[v] = max(mx[v], mx[x]);
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> q;
ll tot = 0;
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({w, v});
g[v].push_back({w, u});
tot += w;
}
while (q--) {
int s, k, t;
cin >> s >> k >> t;
dep[s] = 0;
dfs(-1, s);
for (int v = 1; v <= n; v++) col[v] = 0;
for (int v = t; ; v = par[v]) {
col[v] = 1;
if (v == s) break;
}
if (col[k]) {
cout << 2 * tot - mx[s] << '\n';
continue;
}
int l = k;
while (!col[l]) {
col[l] = 2;
l = par[l];
}
if (l == t) {
cout << "impossible\n";
continue;
}
ll ans = 1e18;
for (int v = t; ; v = par[v]) {
ans = min(ans, 2 * tot - dep[v]);
for (auto [w, x] : g[v]) if (x != par[v] && !col[x]) {
ans = min(ans, 2 * tot - mx[x]);
}
if (v == s) break;
}
for (int v = k; col[v] == 2; v = par[v]) {
ans = min(ans, 2 * tot - dep[v] + 2 * (dep[v] - dep[l]));
for (auto [w, x] : g[v]) if (x != par[v] && !col[x]) {
ans = min(ans, 2 * tot - mx[x] + 2 * (dep[v] - dep[l]));
}
}
cout << ans << '\n';
}
}
詳細信息
Test #1:
score: 100
Accepted
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:
316 293 293 impossible 314
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3616kb
input:
100 100 1 2 289384 2 3 930887 2 4 692778 4 5 636916 4 6 747794 4 7 238336 4 8 885387 8 9 760493 8 10 516650 8 11 641422 8 12 202363 8 13 490028 8 14 368691 8 15 520060 8 16 897764 16 17 513927 16 18 180541 16 19 383427 16 20 89173 16 21 455737 16 22 5212 16 23 595369 16 24 702568 16 25 956430 16 26 ...
output:
103526917 103484292 106288816 104379596 104405611 104775512 105434682 105291604 103838430 105371370 104677980 104175650 105894571 104509242 103971939 105376499 105223283 104153426 105082245 105413188 104130613 104800548 106846555 104138329 103769253 105456754 104044745 104385328 106973740 105460460 ...
result:
ok 100 lines
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3668kb
input:
10 6 1 2 4 2 3 7 2 4 8 4 5 6 4 6 4 4 7 6 4 8 7 8 9 3 8 10 10 3 8 1 10 4 7 1 7 3 7 2 9 8 10 3 4 1 6
output:
99 78 97 87 88 104
result:
wrong answer 6th lines differ - expected: '93', found: '104'