QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#55020 | #1271. In Search of Gold | YaoBIG# | TL | 0ms | 3624kb | C++ | 2.4kb | 2022-10-11 22:28:48 | 2022-10-11 22:28:51 |
Judging History
answer
#include "bits/stdc++.h"
#define rep(i, a, n) for (auto i = a; i <= (n); i++)
#define revrep(i, a, n) for (auto i = n; i >= (a); i--)
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a).size()
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } else return 0; }
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } else return 0; }
using namespace std;
template<class A, class B> string to_string(pair<A, B> p);
template<class A> string to_string(A v) {
bool first = 1;
string res = "{";
for (auto x: v) {
if (!first) res += ", ";
first = 0;
res += to_string(x);
}
res += "}";
return res;
}
template<class A, class B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(H h, T... t) {
cerr << " " << to_string(h);
debug_out(t...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int cas; cin >> cas; while (cas--) {
int n, k; cin >> n >> k;
vector<vector<tuple<int, int, int>>> g(n);
rep(i, 1, n - 1) {
int u, v, a, b; cin >> u >> v >> a >> b;
u--, v--;
g[u].emplace_back(v, a, b);
g[v].emplace_back(u, a, b);
}
auto check = [&](ll guess) {
static vector<vector<ll>> dp;
dp.resize(n);
auto dfs = [&](auto &dfs, int now, int fa) -> void {
auto &cur = dp[now];
cur.assign(1, 0ll);
for (auto [v, a, b]: g[now]) if (v != fa) {
dfs(dfs, v, now);
auto &sondp = dp[v];
vector<ll> ndp(min(sz(dp) + sz(sondp) + 1, k + 1), 1ll << 60);
rep(i, 0, sz(dp) - 1) {
rep(j, 0, sz(sondp) - 1) {
if (i + j > k) break;
if (cur[i] + sondp[j] + b <= guess) chmin(ndp[i + j], max(cur[i], sondp[j] + b));
if (i + j + 1 <= k && cur[i] + sondp[j] + a <= guess) chmin(ndp[i + j + 1], max(cur[i], sondp[j] + a));
}
}
swap(cur, ndp);
}
};
dfs(dfs, 0, -1);
return dp[0][k] != 1ll << 60;
};
ll lo = 0, hi = 1ll * (n - 1) * (ll)(1e9 + 1);
while (lo < hi) {
ll mid = (lo + hi) >> 1;
if (check(mid)) hi = mid;
else lo = mid + 1;
}
printf("%lld\n", hi);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
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
Time Limit Exceeded
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:
942260190 3732632654 2451798440 2087923068 3307453190 4490065261 2393372595 2816978868 2555185013 2411463895 2137224462 1582942446 1213751604 1924820637 2018214075 2362339230 3014059043 2262889657 2303205388 2110653290 3081993716 2849219294 1733366442 2021128541 2303200787 3420317049 2870883724 3104...