QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#50685 | #1271. In Search of Gold | ckiseki# | AC ✓ | 1356ms | 9384kb | C++ | 2.3kb | 2022-09-28 16:39:36 | 2022-09-28 16:39:37 |
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;
fill(dp[i].begin(), dp[i].end(), INF);
dp[i][0] = 0;
for (auto [j, a, b]: g[i]) if (j != pa[i]) {
array<int64_t,maxk> tmp;
fill(tmp.begin(), tmp.end(), INF);
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];
fill(tmp.begin(), tmp.end(), 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: 2ms
memory: 5688kb
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: 0
Accepted
time: 1356ms
memory: 9384kb
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:
1411481343 3753603561 2451798440 2414772415 3307453190 4490065261 4414121261 2816978868 2555185013 3116086232 3159869324 1582942446 1213751604 1927788364 2504746732 2508553278 3014059043 2439597035 2303205388 2110653290 3081993716 3699114788 1916042583 2021128541 2303200787 3850983146 2870883724 319...
result:
ok 1118 lines