QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#244000#1271. In Search of GoldPhantomThreshold#AC ✓1192ms12276kbC++201.8kb2023-11-08 20:12:292023-11-08 20:12:29

Judging History

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

  • [2023-11-08 20:12:29]
  • 评测
  • 测评结果:AC
  • 用时:1192ms
  • 内存:12276kb
  • [2023-11-08 20:12:29]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;

void upd(int &a,const int &b){ if(a==-1||a>b)a=b; }
const int maxn = 21000;
const int maxk = 22;
const int inf  = 1e15;

int n,K;
vector< tuple<int,int,int> >E[maxn];
int ff[maxn],fa[maxn],fb[maxn],siz[maxn];

void dfs(const int x)
{
	siz[x]=1;
	for(auto [y,a,b]:E[x]) if(y!=ff[x])
	{
		ff[y]=x;
		fa[y]=a,fb[y]=b;
		dfs(y);
		siz[x]+=siz[y];
	}
}

int f[maxn][maxk],nf[maxk];
int dp(const int x,const int mid)
{
	vector<int>son;
	for(auto [y,a,b]:E[x]) if(y!=ff[x])
	{
		if(!dp(y,mid)) return 0;
		son.push_back(y);
	}
	
	memset(f[x],-1,sizeof f[x]);
	f[x][0]=0;
	int sx=1;
	for(auto y:son)
	{
		memset(nf,-1,sizeof nf);
		for(int kx=0;kx<=K&&kx<sx;kx++) if(f[x][kx]!=-1)
		{
			for(int ky=0;kx+ky<=K&&ky<=siz[y];ky++) if(f[y][ky]!=-1)
			{
				if(f[x][kx]+f[y][ky]<=mid)
					upd(nf[kx+ky],max(f[x][kx],f[y][ky]));
			}
		}
		memcpy(f[x],nf,sizeof f[x]);
		sx+=siz[y];
	}
	if(x==1&&f[x][K]==-1) return 0;
	
	for(int k=K;k>=0;k--)
	{
		int ka= (k>0&&f[x][k-1]!=-1)?f[x][k-1]+fa[x]:inf;
		int kb= (f[x][k]!=-1)?f[x][k]+fb[x]:inf;
		
		f[x][k]=min(ka,kb);
		if(f[x][k]>mid) f[x][k]=-1;
	}
	
	return 1;
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int Tcase; cin>>Tcase;
	while(Tcase--)
	{
		cin>>n>>K;
		for(int i=1;i<=n;i++)
		{
			vector< tuple<int,int,int> >_;
			E[i].swap(_);
		}
		
		int sum=0;
		for(int i=1;i<n;i++)
		{
			int x,y,a,b; cin>>x>>y>>a>>b;
			E[x].emplace_back(y,a,b);
			E[y].emplace_back(x,a,b);
			sum+=max(a,b);
		}
		ff[1]=0; fa[1]=fb[1]=0;
		dfs(1);
		
		int l=1,r=sum;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if( dp(1,mid) ) r=mid-1;
			else l=mid+1;
		}
		cout<<r+1<<'\n';
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1192ms
memory: 12276kb

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