UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#146867#9. 小h的树uuku100201ms107584kbC++112.3kb2022-03-06 14:41:352022-03-06 14:41:36

answer

#include<bits/stdc++.h>
#define ll long long
#define ls (rt<<1)
#define rs (rt<<1|1)
#define pb  push_back
#define mp make_pair
using namespace std;
const int LEN=1<<21;
char BUF[LEN],*Pin,*Pin_last,PUF[LEN],*Pout=PUF,*Pout_last=PUF+LEN-1;
inline char G(){return Pin==Pin_last&&(Pin_last=(Pin=BUF)+fread(BUF,1,LEN,stdin),Pin==Pin_last)?EOF:*Pin++;}
inline void P(char x){if(Pout==Pout_last)fwrite(PUF,1,Pout-PUF,stdout),Pout=PUF;*Pout++=x;}
inline int read(){int res=0,f=1,ch=' ';for(;(ch<'0'||ch>'9')&&ch!=EOF;ch=G())if(ch=='-')f=-1;for(;ch>='0'&&ch<='9';ch=G())res=(res<<3)+(res<<1)+ch-48;return res*f;}
inline ll readL(){ll res=0,f=1,ch=' ';for(;(ch<'0'||ch>'9')&&ch!=EOF;ch=G())if(ch=='-')f=-1;for(;ch>='0'&&ch<='9';ch=G())res=(res<<3)+(res<<1)+ch-48;return res*f;}
inline void wtL(ll a){if(a>9)wtL(a/10);P(a%10+'0');return;}
inline void writeL(ll a,char b){if(a<0)P('-'),a=-a;wtL(a),P(b);}
inline void wt(int a){if(a>9)wt(a/10);P(a%10+'0');return;}
inline void write(int a,char b){if(a<0)P('-'),a=-a;wt(a),P(b);}
inline void wt_str(char s[],int len=0){if(!len)len=strlen(s);for(int i=0;i<len;i++)P(s[i]);return;}
const int N=3e3+10;
struct Edge{int to,next,dis;}e[N<<1];
int head[N],num,n,k,siz[N],f[N][N][3],ans=2e9;
//f[i][j][k]  iµÄ×ÓÊ÷ÖÐÑ¡ÁËj¸ö£¬ÆðµãÖյ㹲k¸ö 
inline void add(int x,int y,int z){return e[++num]=(Edge){y,head[x],z},head[x]=num,void();}
inline void chkmin(int &x,int y){return (x>y?x=y:0),void();} 
void dfs(int now,int fa)
{
	f[now][1][0]=f[now][1][1]=0,siz[now]=1;
	for(int p=head[now],to,vl;p;p=e[p].next)
		if((to=e[p].to)!=fa)
		{
			dfs(to,now),vl=e[p].dis;
			for(int j=siz[now];j;j--)
				for(int z=siz[to];z;z--)
				{
					chkmin(f[now][j+z][0],f[now][j][0]+f[to][z][0]+vl+vl);
					chkmin(f[now][j+z][1],f[now][j][0]+f[to][z][1]+vl);
					chkmin(f[now][j+z][1],f[now][j][1]+f[to][z][0]+vl+vl);
					chkmin(f[now][j+z][2],f[now][j][2]+f[to][z][0]+vl+vl);
					chkmin(f[now][j+z][2],f[now][j][0]+f[to][z][2]+vl+vl);
					chkmin(f[now][j+z][2],f[now][j][1]+f[to][z][1]+vl);
				}
			siz[now]+=siz[to];
		}
	ans=min(ans,f[now][k][2]);
	return;
}
int main()
{
	n=read(),k=read();
	for(int i=1,x,y,z;i<n;i++)
		x=read(),y=read(),z=read(),
		add(x,y,z),add(y,x,z);
	memset(f,0x7f/2,sizeof(f));
	dfs(1,0);
	cout<<ans<<'\n';
	fwrite(PUF,1,Pout-PUF,stdout);
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 12ms
memory: 107340kb

input:

10 7
1 2 35129
2 3 42976
3 4 24497
2 5 83165
1 6 4748
5 7 38311
4 8 70052
3 9 3561
8 10 80238

output:

184524

result:

ok 1 number(s): "184524"

Test #2:

score: 10
Accepted
time: 28ms
memory: 107468kb

input:

3000 3000
1 2 80232
1 3 31904
1 4 24318
1 5 70898
1 6 54285
1 7 99203
1 8 36858
1 9 52269
1 10 21554...

output:

293195939

result:

ok 1 number(s): "293195939"

Test #3:

score: 10
Accepted
time: 8ms
memory: 107348kb

input:

50 32
1 2 78088
1 3 59372
2 4 12219
3 5 17188
2 6 51314
3 7 72028
3 8 1593
4 9 65442
5 10 97450
6 11...

output:

1758096

result:

ok 1 number(s): "1758096"

Test #4:

score: 10
Accepted
time: 15ms
memory: 107344kb

input:

50 48
1 2 60240
2 3 71246
3 4 21134
2 5 75461
5 6 4274
6 7 87233
6 8 74521
4 9 81504
8 10 23918
5 11...

output:

3245532

result:

ok 1 number(s): "3245532"

Test #5:

score: 10
Accepted
time: 8ms
memory: 107348kb

input:

200 36
1 2 95675
1 3 46068
3 4 92249
2 5 79443
4 6 11920
6 7 28895
4 8 25231
5 9 82622
8 10 34349
8 ...

output:

1214282

result:

ok 1 number(s): "1214282"

Test #6:

score: 10
Accepted
time: 0ms
memory: 107352kb

input:

200 35
1 2 97551
2 3 27011
2 4 14709
1 5 27407
2 6 57345
5 7 63132
7 8 89143
4 9 30986
5 10 89545
7 ...

output:

1341305

result:

ok 1 number(s): "1341305"

Test #7:

score: 10
Accepted
time: 32ms
memory: 107464kb

input:

3000 666
1 2 65806
1 3 53490
1 4 43794
1 5 63048
1 6 10670
1 7 56872
1 8 71919
1 9 78642
1 10 9831
1...

output:

14876698

result:

ok 1 number(s): "14876698"

Test #8:

score: 10
Accepted
time: 28ms
memory: 107584kb

input:

3000 1633
1 2 73534
2 3 97254
3 4 96323
4 5 88456
5 6 287
6 7 7380
7 8 63246
8 9 57273
9 10 23915
10...

output:

74678010

result:

ok 1 number(s): "74678010"

Test #9:

score: 10
Accepted
time: 42ms
memory: 107464kb

input:

3000 2612
1 2 85560
1 3 99187
1 4 40779
1 5 70190
1 6 50395
1 7 9810
1 8 87504
1 9 600
1 10 78858
1 ...

output:

231082152

result:

ok 1 number(s): "231082152"

Test #10:

score: 10
Accepted
time: 28ms
memory: 107548kb

input:

3000 858
1 2 51625
1 3 28801
1 4 85825
3 5 92506
5 6 76389
4 7 74801
3 8 49889
6 9 81981
9 10 73122
...

output:

38161665

result:

ok 1 number(s): "38161665"

Extra Test:

score: 0
Extra Test Passed