求助16pts
查看原帖
求助16pts
232507
OK咯莫名其妙楼主2021/8/23 10:45
#include<bits/stdc++.h>
using namespace std;
const int maxn=15000;
struct node{
	int u,v,w,nxt;
}edge[maxn];
int head[maxn],n,q,val[maxn],tot,f[maxn][maxn];
void add(int u,int v,int w){
	edge[++tot].u=u;
	edge[tot].v=v;
	edge[tot].w=w;
	edge[tot].nxt=head[u];
	head[u]=tot;
}
void dfs(int u,int t){
	if(t<=0) return ;
//	cout<<1<<endl;
	for(int i=head[u];i;i=edge[i].nxt) {
		int v=edge[i].v;
		if(v!=u){	
		for(int k=0;k<t;k++)
			f[v][k]=f[u][k]+edge[i].w;
		dfs(v,t-1);
		for(int k=1;k<=t;k++){
			f[u][k]=max(f[u][k],f[v][k-1]);
			//cout<<f[u][k]<<endl;	
		}
		}
	
	}
}
int main(){
	cin>>n>>q;
	for(int i=1;i<n;i++){
		int x,y,w;
		cin>>x>>y>>w;
		add(x,y,w);
		add(y,x,w);
	}
	dfs(1,q);
	cout<<f[1][q]<<endl;
	return 0;
}
2021/8/23 10:45
加载中...