hdu5148---Cities

Cities

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 355    Accepted Submission(s): 122

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

BestCoder Round #23

Recommend

heyang   |   We have carefully selected several similar problems for you:  5149 5145 5144 5143 5142

题目要求 sigma(i) sigma(j) d[i][j]  / k^2

转换一下就是求sigma(i) sigma(j) d[i][j]

根据题解所说, 每条边会把树分成2份,如果在一部分里选j个点,那么这条边贡献了 j * (k - j) * w * 2的值

所以可以令 dp[u][i] 表示 在以 u为根的子树里选i个点,

dp[u][i] = min(dp[v][j] + dp[u][i- j] + cost);

然后树形dp搞下就行了,为了使dp[u][i- j] 里 不包括 dp[v][j], 在枚举i的时候要逆序,原因和01背包的空间优化原理一样

/*************************************************************************
    > File Name: hdu5148.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2014年12月23日 星期二 18时04分08秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2010;

int n, k, tot;
long long dp[N][55];
int head[N];

struct node
{
	int weight;
	int next;
	int to;
}edge[N << 1];

void addedge(int from, int to, int weight)
{
	edge[tot].weight = weight;
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

void dfs(int u, int fa)
{
	dp[u][0] = 0;
	dp[u][1] = 0;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		int w = edge[i].weight;
		if (v == fa)
		{
			continue;
		}
		dfs(v, u);
		for (int i = k; i >= 1; --i)
		{
			for (int j = 1; j <= i; ++j)
			{
				long long cost = (long long)2 * j * (k - j) * w;
				dp[u][i] = min(dp[u][i], dp[u][i - j] + dp[v][j] + cost);
			}
		}
	}
}

int main()
{
	int t;
	int u, v, w;
	scanf("%d", &t);
	while (t--)
	{
		memset (head, -1, sizeof(head));
		tot = 0;
		scanf("%d%d", &n, &k);
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 0; j <= k; ++j)
			{
				dp[i][j] = (1ll << 60);
			}
		}
		for (int i = 1; i <= n - 1; ++i)
		{
			scanf("%d%d%d", &u, &v, &w);
			addedge(u, v, w);
			addedge(v, u, w);
		}
		dfs(1, -1);
		printf("%lld\n", dp[1][k]);
	}
	return 0;
}
时间: 2024-08-04 19:09:07

hdu5148---Cities的相关文章

hdu-2874 Connections between cities(lca+tarjan+并查集)

题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, s

HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct node{ int u; int v; int w; }que[100000]; int father[505]; bool cmp(struct node a,struct node b){ return a.w<b.w;

Occupy Cities

hdu4606:http://acm.hdu.edu.cn/showproblem.php?pid=4606 题意:在一个二维坐标系中,有n个城市,坐标给出来了,然后有p个士兵要去占领这n个城市,但是路上有m个路障,都是线段,士兵不能越过路障前进. 每个士兵都有相同容量大小的一个干粮袋,每到一个城市他就能补充满自己的干粮袋.中途走路时走一个单位长度就消耗一个单位的干粮. 现在问的是这些个干粮袋最小的容量是多少,前提是保证p个士兵能占领完这n个城市,城市被占领顺序也是题目给好的,必须遵守. 题解:

hdu 3371 Connect the Cities

链接:hdu 3371 已知已连通的路的序号,以及未连通的路的费用,求将所有城市连通的最小费用 也是将已连通的路的费用记为0,就转化成了基本最小生成树的题 不过这题数组要开的大点,不然很容易就RE了... #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[100000]; int cmp(struct stu x,struct st

1013. Battle Over Cities (25)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of

Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的

Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 701 Accepted Submission(s): 212   Problem Description In 2100, since the sea level rise, most of the cities disappear. Though som

codeforces 613D:Kingdom and its Cities

Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daug

HDU 3371 Connect the Cities 【最小生成树,Prime算法+Kruskal算法】

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17167    Accepted Submission(s): 4335 Problem Description In 2100, since the sea level rise, most of the cities disappear. Tho

hdu 5148 Cities(树形dp)

题目链接:hdu 5148 Cities dp[i][j]表示以i为根节点,选j个最优值,每条边被选中的时候就计算出被经过的次数,并乘上权值. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; typedef pair<int, int> pii; typedef long long ll; const

1013. Battle Over Cities (25)(连通分量个数 、 并查集)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of