QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#642161#1271. In Search of GoldqyzyAC ✓668ms16292kbC++142.1kb2024-10-15 11:13:362024-10-15 11:13:36

Judging History

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

  • [2024-10-15 11:13:36]
  • 评测
  • 测评结果:AC
  • 用时:668ms
  • 内存:16292kb
  • [2024-10-15 11:13:36]
  • 提交

answer

/*Life is too long to end at a grave*/
#include<bits/stdc++.h>
#define N 100010
#define I_love_Furina return
#define forever 0
#define foreverr 1
#define endl '\n'
#define FIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define db double
#define ull unsigned long long
#define chc cout<<114514<<endl
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define int long long
#define mid (r+l>>1)
using namespace std;
int n,T,m,dp[N][31],maxlen[31],son[N],head[N],num;
struct edge{
    int to,nxt,aw,bw;
}a[N<<1];
inline void add(int u,int v,int aw,int bw){
    a[++num].to=v,a[num].aw=aw,a[num].bw=bw,a[num].nxt=head[u],head[u]=num;
}
void dfs(int x,int fa,int lim){
    dp[x][0]=son[x]=0;
    for(int k=head[x];k;k=a[k].nxt){
        int v=a[k].to,aw=a[k].aw,bw=a[k].bw;
        if(v==fa)continue;
        dfs(v,x,lim);
        int so=min(son[x]+son[v]+1,m);
        for(int j=0;j<=so;j++)maxlen[j]=1e18;
        for(int i=0;i<=son[x];i++)
            for(int j=0;j<=son[v]&&i+j<=m;j++){
                if(dp[x][i]+dp[v][j]+aw<=lim)maxlen[i+j+1]=min(maxlen[i+j+1],max(dp[x][i],dp[v][j]+aw));
                if(dp[x][i]+dp[v][j]+bw<=lim)maxlen[i+j]=min(maxlen[i+j],max(dp[x][i],dp[v][j]+bw));
            }
        son[x]=so;
        for(int j=0;j<=so;j++)dp[x][j]=maxlen[j];
    }   
}
inline bool check(int x){
    // memset(maxlen,0x3f,sizeof maxlen);
    //memset(dp,0,sizeof dp);
    //memset(son,0,sizeof son);
    dfs(1,0,x);
    I_love_Furina (dp[1][m]<=x);
}
signed main(){
    IOS;//FIO("");
    cin>>T;
    while(T--){
        cin>>n>>m;
        int l=0,r=0;
        num=0;for(int i=1;i<=n;i++)head[i]=0;
        for(int i=1,u,v,a,b;i<n;i++)cin>>u>>v>>a>>b,r+=max(a,b),add(u,v,a,b),add(v,u,a,b);
        
        while(r-l>2){
            if(check(mid))r=mid;
            else l=mid;
        }
        if(check(l))cout<<l;
        else if(check(mid))cout<<mid;
        else cout<<r;
cout<<endl;
        // dfs(1,0);
        // cout<<maxlen[1][m]<<endl;
    }
    I_love_Furina forever;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 668ms
memory: 16292kb

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