QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#505833#1271. In Search of GoldseungniAC ✓3480ms11744kbC++201.8kb2024-08-05 12:08:002024-08-05 12:08:01

Judging History

你现在查看的是最新测评结果

  • [2024-08-05 12:08:01]
  • 评测
  • 测评结果:AC
  • 用时:3480ms
  • 内存:11744kb
  • [2024-08-05 12:08:00]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

ll N, K, D;
vector <pair<int, pii>> tree[20005];
ll dp[20005][25];

void init() {
    for(int i = 1; i <= N; i++) tree[i].clear();
}

void DFS(int node, int par) {
    dp[node][0] = 0;
    for(int i = 1; i <= K; i++) dp[node][i] = 1e18;

    for(int i = 0; i < tree[node].size(); i++) {
        ll next = tree[node][i].first, a = tree[node][i].second.first, b = tree[node][i].second.second;
        if(next == par) continue;
        DFS(next, node);

        for(int j = K; j >= 1; j--) dp[next][j] = min(dp[next][j - 1] + a, dp[next][j] + b);
        dp[next][0] += b;

        vector <ll> v(25, 1e18);

        for(int j = 0; j <= K; j++) {
            for(int k = 0; k <= K; k++) {
                if(j + k > K) break;
                if(dp[node][j] + dp[next][k] <= D) v[j + k] = min(v[j + k], max(dp[node][j], dp[next][k]));
            }
        }

        for(int j = 0; j <= K; j++) dp[node][j] = v[j];
    }

    return;
}

void solve() {
    cin >> N >> K;

    init();

    for(int i = 0; i < N - 1; i++) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        tree[a].push_back({b, {c, d}});
        tree[b].push_back({a, {c, d}});
    }

    ll l = 0, r = 1e15, ok = -1;
    while(l <= r) {
        ll m = (l + r) / 2;
        D = m;
        DFS(1, 1);
        if(dp[1][K] < 1e18) {
            ok = m;
            r = m - 1;
        }
        else l = m + 1;
    }

    cout << ok << '\n';

    return;
}

int main(void) {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3648kb

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: 3480ms
memory: 11744kb

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