(树形DP) hdu 5148

Cities

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

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

dp[i][j]用来表示 以i为根节点的子树中选J个点的最小代价

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
#define LL long long
const long long INF=20000*100000000LL;
vector<int> e[2010],w[2010];
int n,k;
LL dp[2010][100],temp[100];
void dfs(int u,int fa)
{
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v==fa)
            continue;
        dfs(v,u);
        for(int j=0;j<=k;j++)
            temp[j]=dp[u][j];
        for(int j=0;j<=k;j++)
        {
            for(int t=0;t<=j;t++)
                temp[j]=min(temp[j],dp[u][j-t]+dp[v][t]+t*(k-t)*w[u][i]*2);
        }
        for(int j=0;j<=k;j++)
            dp[u][j]=temp[j];
    }
}
int main()
{
    int tt,x,y,z;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
        {
            e[i].clear();
            w[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            e[x].push_back(y);
            e[y].push_back(x);
            w[x].push_back(z);
            w[y].push_back(z);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=k;j++)
            {
                if(j<=1)
                    dp[i][j]=0;
                else
                    dp[i][j]=INF;
            }
        }
        dfs(1,-1);
        printf("%I64d\n",dp[1][k]);
    }
    return 0;
}

  

时间: 2024-10-12 16:08:14

(树形DP) hdu 5148的相关文章

fwt优化+树形DP HDU 5909

1 //fwt优化+树形DP HDU 5909 2 //见官方题解 3 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ 4 5 #include <bits/stdc++.h> 6 // #include <iostream> 7 // #include <cstdio> 8 // #include <cstdlib> 9 // #include <algorithm> 10 // #inc

树形dp/hdu 1011 Starship Troopers

题意 有n个房子,1号为起点房子.每个房子里会消耗一定的士兵来获取一定的价值.现在有m个士兵,求问可以获得的最大价值 注意:走过的房子不能再走 注意2:若要消灭这个房子的bugs,必须全部消灭 分析 典型的树形dp,01背包,因为每个房子里要么全杀死bugs,要么一个不动,只有取或不取两种状态 设f[i][j]表示以i为根节点,消耗j个士兵所能获得的最大价值 则f[i][j]=max(f[son[i]][k] + f[i][j-k]); 答案为f[1][m] Accepted Code 1 /*

树形DP [HDU 2196] Computer

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3526    Accepted Submission(s): 1788 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D

树形DP [HDU 1561] The more, The Better

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5506    Accepted Submission(s): 3274 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

(树形DP) hdu 1561

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5822    Accepted Submission(s): 3469 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物

(树形DP) hdu 1520

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5619    Accepted Submission(s): 2580 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

(树形DP) hdu 3452

Bonsai Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 783    Accepted Submission(s): 395 Problem Description After being assaulted in the parking lot by Mr. Miyagi following the "All Valley Kar

(树形DP) hdu 2196

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3982    Accepted Submission(s): 1996 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

hdu 5148 树形dp+分组背包问题

http://acm.hdu.edu.cn/showproblem.php?pid=5148 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 Ja