QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#26106#1271. In Search of GoldWu_RenAC ✓1024ms9340kbC++171.1kb2022-04-06 15:30:532022-04-29 02:55:53

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-29 02:55:53]
  • 评测
  • 测评结果:AC
  • 用时:1024ms
  • 内存:9340kb
  • [2022-04-06 15:30:53]
  • 提交

answer

#include <bits/stdc++.h>
typedef long long ll;
const ll inf=1e18;
using namespace std;
int T,n,k,head[20010],o=0,sz[20010];
ll x,f[20010][21],g[21];
struct edge{
	int to,link,a,b;
}e[40010];
void add_edge(int u,int v,int a,int b){
	e[++o]={v,head[u],a,b},head[u]=o;
	e[++o]={u,head[v],a,b},head[v]=o;
}
void dfs(int u,int pre){
	for(int i=1;i<=k;i++) f[u][i]=inf;
	f[u][0]=0,sz[u]=1;
	for(int i=head[u],v;i;i=e[i].link) if((v=e[i].to)^pre){
		dfs(v,u);
		for(int j=k;j>=0;j--) f[v][j]=min(f[v][j]+e[i].b,(j>0?f[v][j-1]:inf)+e[i].a);
		for(int j=0;j<=k;j++) g[j]=inf;
		for(int j=0;j<=min(sz[v],k);j++) for(int l=0;l<sz[u]&&j+l<=k;l++) if(f[v][j]+f[u][l]<=x){
			g[j+l]=min(g[j+l],max(f[v][j],f[u][l]));
		}
		for(int j=0;j<=k;j++) f[u][j]=g[j];
		sz[u]+=sz[v];
	}
}
void sol(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++) head[i]=0;o=0;
	for(int i=1,u,v,a,b;i<n;i++) scanf("%d%d%d%d",&u,&v,&a,&b),add_edge(u,v,a,b);
	ll l=0,r=2e13,ans=r;
	while(l<=r){
		x=(l+r)>>1;
		dfs(1,0);
		if(f[1][k]<=x) ans=x,r=x-1;
		else l=x+1;
	}
	printf("%lld\n",ans);
}
int main(){
	scanf("%d",&T);
	while(T--) sol();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3772kb

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: 1024ms
memory: 9340kb

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