HDU 5148(Cities-树上背包)

Cities

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 670    Accepted Submission(s): 198


Problem Description
Long long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it's a tree).The king wants to reward JayYe because he beats the devil and save the princess.The king decide to give JayYe exactly K cities as his daughter's dowry.
Here comes the question.Although JayYe beats the devil,his knee was injured.So he doesn't want to move too much,he wants his citys as close as possible,that is, JayYe wants the expected distance of his cities as small as possible.
The expected distance is defined as the expected distance between node u and node v,both u and v are randomly choose from JayYe's K cities equiprobably(that means first choose u randomly from JayYe’s K cities,then choose v randomly from JayYe’s K cities,so the case u equals to v is possible).
Suppose you are the king,please determine the K cities so that JayYe is happy.
Because the author is lazy,you only need tell me the minimum expect distance.
 

Input
The first line contains a single integer T,indicating the number of test cases.
Each test case begins with two integers n and K,indicating the number of cities in this country,the number of cities the king gives to the knight.Then follows n-1 lines,each line contains three integers a,b,and c, indicating there is a road connects city a and city b with length c.

[Technical Specification]
1 <= T <= 100
1 <= K <= min(50,n)
1 <= n <= 2000
1 <= a,b <= n
0 <= c <= 100000
 

Output
For each case, output one line, contain one integer, the minimum expect distance multiply K2 .
 

Sample Input
  
  
1 2 2 1 2 1
 

Sample Output
  
  
2
 

Source
 

Recommend
heyang   |   We have carefully selected several similar problems for you:   5231  5230  5229  5228  5227 
 

易证树上所取子图连通

令dp[i,j]表示子树i取j个节点

则对u树的子树v  u-v 所取次数=2*子树v所取节点数*(K-子树v所取节点数)  K同题意

那么它满足 最优子结构


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<vector>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define F (100000007)
#define MAXN (2000+10)
#define MAXK (100+10)
#define MAXc (100000+10) 
#define INF (1000000000000LL)
typedef long long ll;

int n,k;
ll dp[MAXN][MAXK];
vector<int> edge[MAXN],dis[MAXN];
void dfs(int u,int fa)
{
	int sz=edge[u].size();
	Rep(i,sz)
	{
		int v=edge[u][i];
		ll d=dis[u][i];
		if (v!=fa)
		{
			dfs(v,u);
			ForD(j,k)
			{
				ForD(t,j-1) //至少取1个节点 
					dp[u][j]=min(dp[u][j],dp[u][t]+dp[v][j-t]+(j-t)*(k-(j-t))*d*2);
			}
			
			
		}
	}
}
int main()
{
//	freopen("1003.in","r",stdin);
//	freopen(".out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&k);
		For(i,n) dis[i].resize(0);
		For(i,n) edge[i].resize(0);
		For(i,n-1)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			edge[a].push_back(b);
			edge[b].push_back(a);
			dis[a].push_back(c);
			dis[b].push_back(c);
		}
		
		MEM(dp)
		For(i,n)
			Fork(j,2,k)
			{
				dp[i][j]=(ll)INF;
			}
		
		dfs(1,-1);
		ll ans=INF;
		For(i,n) ans=min(ans,dp[i][k]);
		printf("%I64d\n",ans);	
	}
	
		
	return 0;
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值