QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#50661 | #1271. In Search of Gold | ckiseki# | WA | 1520ms | 9340kb | C++ | 2.2kb | 2022-09-28 14:43:46 | 2022-09-28 14:43:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 20025;
const int maxk = 21;
const int64_t INF = 1e18;
vector<tuple<int,int,int>> g[maxn];
vector<int> ord;
int pa[maxn];
void dfs(int i, int f) {
pa[i] = f;
ord.push_back(i);
for (auto [j, a, b]: g[i]) if (j != f) {
dfs(j, i);
}
}
array<int64_t,maxk> dp[maxn];
int sz[maxn];
bool ok(int64_t diam, int k) {
for (int i: ord) {
sz[i] = 1;
dp[i][0] = 0;
for (auto [j, a, b]: g[i]) if (j != pa[i]) {
array<int64_t,maxk> tmp;
for (int x = 0; x < sz[j]; x++)
tmp[x] = dp[j][x] + b;
for (int x = 0; x < sz[j] && x + 1 < maxk; x++)
tmp[x + 1] = min(tmp[x + 1], dp[j][x] + a);
sz[j] = min(maxk, sz[j] + 1);
for (int x = 0; x < sz[j]; x++)
dp[j][x] = tmp[x];
for (int x = 0; x < sz[i] + sz[j] && x < maxk; x++)
tmp[x] = INF;
for (int x = 0; x < sz[i]; x++)
for (int y = 0; y < sz[j] && x + y < maxk; y++)
if (dp[i][x] + dp[j][y] <= diam)
tmp[x + y] = min(tmp[x + y], max(dp[i][x], dp[j][y]));
sz[i] = min(maxk, sz[i] + sz[j]);
for (int x = 0; x < sz[i]; x++)
dp[i][x] = tmp[x];
}
}
return dp[1][k] < INF;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t; cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
g[i].clear();
ord.clear();
for (int i = 1; i < n; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
g[u].emplace_back(v, a, b);
g[v].emplace_back(u, a, b);
}
dfs(1, 0);
reverse(ord.begin(), ord.end());
// string v;
// for (int x = 0; x < 10; x++) {
// v += char('0' + ok(x, k));
// }
// cerr << v;
// cerr << endl;
// continue;
int64_t x = -1;
for (int64_t s = 1LL << 50; s; s >>= 1) { // TODO
if (not ok(x + s, k)) {
x += s;
}
}
x += 1;
cout << x << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 6064kb
input:
1 4 1 1 2 1 3 2 3 4 2 2 4 3 5
output:
6
result:
ok single line: '6'
Test #2:
score: -100
Wrong Answer
time: 1520ms
memory: 9340kb
input:
1118 10 5 1 2 557878805 99156035 2 3 170460737 198842212 3 4 473592718 245654078 4 5 774731915 3786984 1 6 817584282 305447690 1 7 308601560 633840726 3 8 718662215 102379861 3 9 26761157 849561804 6 10 617758160 117754666 10 6 1 2 952221943 224077095 2 3 101056818 462900286 3 4 760307950 560511059 ...
output:
1170198830 3753603561 2451798440 2414772415 3307453190 4490065261 2889922413 2816978868 2555185013 2697609693 1748263430 1582942446 1081864989 1927788364 2080374784 2508553278 3014059043 2439597035 2303205388 1900538150 2945958786 3613561286 1878008918 1600803667 2292496462 3721391382 2870883724 319...
result:
wrong answer 1st lines differ - expected: '1411481343', found: '1170198830'