QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#643504 | #1271. In Search of Gold | 5toubun_no_hanayome | AC ✓ | 891ms | 10052kb | C++14 | 3.6kb | 2024-10-15 21:29:02 | 2024-10-15 21:29:11 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a);i <= (b);++i)
#define per(i, a, b) for(int i = (a);i >= (b);--i)
#define lc (k << 1)
#define rc (k << 1 | 1)
#define lowbit(x) ((x) & -(x))
#define odd(x) ((x) & 1)
#define even(x) (!odd(x))
#define fir first
#define sec second
#define pb push_back
#define il inline
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define i128 __int128
#define f128 __float128
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define SZ(x) ((int)(x).size())
#define debug(x) cerr << "In Line " << __LINE__ << " the " << #x << " = " << (x) << "\n"
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<class T> using vc = vector<T>;
template<class Tx, class Ty>
il void chkmx(Tx& x, const Ty y) {x = max<common_type_t<Tx, Ty>>(x, y);}
template<class Tx, class Ty>
il void chkmn(Tx& x, const Ty y) {x = min<common_type_t<Tx, Ty>>(x, y);}
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3fll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
bool ms;
const int N = 20005, M = 25;
int head[N], cnt;
int dep[N], sz[N];
int a[N], b[N];
ll f[N][M], tmp[M];
int k;
struct node {
int to, nxt;
} e[N << 1];
void init(int n) {
cnt = 0;
memset(head, -1, sizeof(head[0]) * (n + 1));
}
void add(int u, int v) {
e[cnt] = {v, head[u]};
head[u] = cnt++;
}
void adde(int u, int v) {
add(u, v);
add(v, u);
}
void dfs1(int u, int f) {
sz[u] = 1;
for(int i = head[u];~i;i = e[i].nxt) {
int v = e[i].to;
if(v == f)
continue;
dep[v] = dep[u] + 1;
dfs1(v, u);
sz[u] += sz[v];
}
}
void dfs2(int u, int fa, ll lim) {
f[u][0] = 0;
int s = 1;
for(int i = head[u];~i;i = e[i].nxt) {
int v = e[i].to;
if(v == fa)
continue;
dfs2(v, u, lim);
memset(tmp, 0x3f, sizeof(tmp));
rep(j, 0, min(s - 1, k)) {
rep(l, 0, min(sz[v] - 1, k)) {
if(j + l > k)
break;
if(f[u][j] + f[v][l] + b[v] <= lim)
chkmn(tmp[j + l], max(f[u][j], f[v][l] + b[v]));
if(j + l + 1 <= k && f[u][j] + f[v][l] + a[v] <= lim)
chkmn(tmp[j + l + 1], max(f[u][j], f[v][l] + a[v]));
}
}
s += sz[v];
rep(j, 0, min(s - 1, k))
f[u][j] = min(tmp[j], llinf);
}
}
struct Edge {
int u, v, a, b;
} edge[N];
void solve() {
int n;
cin >> n >> k;
init(n);
rep(i, 1, n - 1) {
int u, v, a, b;
cin >> u >> v >> a >> b;
adde(u, v);
edge[i] = {u, v, a, b};
}
dep[1] = 1;
dfs1(1, 1);
rep(i, 1, n - 1) {
int u = edge[i].u, v = edge[i].v, a = edge[i].a, b = edge[i].b;
if(u < v)
swap(u, v);
::a[u] = a, ::b[u] = b;
}
auto check = [&](ll mid) -> bool {
dfs2(1, 1, mid);
return f[1][k] <= mid;
};
ll l = 1, r = 1e18, ans = r;
while(l <= r) {
ll mid = l + r >> 1;
if(check(mid)) {
ans = mid;
r = mid - 1;
} else
l = mid + 1;
}
cout << ans << "\n";
}
bool mt;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cerr << fabs(&ms - &mt) / 1024 / 1024 << "\n";
int t;
cin >> t;
while(t--)
solve();
cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4032kb
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: 891ms
memory: 10052kb
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