QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#471852#1271. In Search of Goldgrass8cow#AC ✓2154ms11704kbC++171.2kb2024-07-11 10:10:102024-07-11 10:10:11

Judging History

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

  • [2024-07-11 10:10:11]
  • 评测
  • 测评结果:AC
  • 用时:2154ms
  • 内存:11704kb
  • [2024-07-11 10:10:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[20100][30];
int n,m;
#define pb push_back
#define vi array<int,3>
vector<vi>g[20100];
ll D,fz[30];
const ll I=1e18;
void dfs(int x,int f){
    dp[x][0]=0;
    for(int i=1;i<=m;i++)dp[x][i]=I;
    for(vi t:g[x])if(t[0]!=f){
        int v=t[0];dfs(v,x);
        for(int i=m;i;i--)dp[v][i]=min(dp[v][i-1]+t[1],dp[v][i]+t[2]);
        dp[v][0]+=t[2];
        for(int i=0;i<=m;i++)fz[i]=I;
        for(int i=0;i<=m;i++)
            for(int j=0;i+j<=m;j++)
            if(dp[x][i]+dp[v][j]<=D)
                fz[i+j]=min(fz[i+j],max(dp[x][i],dp[v][j]));
        for(int i=0;i<=m;i++)dp[x][i]=fz[i];
    }
}
bool chk(){
    dfs(1,0);
    return dp[1][m]<I;
}
void sol(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)g[i].clear();
    for(int i=1,u,v,a,b;i<n;i++){
        scanf("%d%d%d%d",&u,&v,&a,&b);
        g[u].pb({v,a,b});
        g[v].pb({u,a,b});
    }
    ll l=0,r=1e15,at=r+1;
    while(l<=r){
        ll mi=(l+r)>>1;D=mi;
        if(chk())at=mi,r=mi-1;
        else l=mi+1;
    }
    printf("%lld\n",at);
}
int main(){
    int T;scanf("%d",&T);while(T--)sol();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4420kb

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: 2154ms
memory: 11704kb

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