HDU - 4118 Holiday's Accommodation

Problem Description

Nowadays, people have many ways to save money on accommodation when they are on vacation.

One of these ways is exchanging houses with other people.

Here is a group of N people who want to travel around the world. They live in different cities, so they can travel to some other people‘s city and use someone‘s house temporary. Now they want to make a plan that choose a destination for each person. There are
2 rules should be satisfied:

1. All the people should go to one of the other people‘s city.

2. Two of them never go to the same city, because they are not willing to share a house.

They want to maximize the sum of all people‘s travel distance. The travel distance of a person is the distance between the city he lives in and the city he travels to. These N cities have N - 1 highways connecting them. The travelers always choose the shortest
path when traveling.

Given the highways‘ information, it is your job to find the best plan, that maximum the total travel distance of all people.

Input

The first line of input contains one integer T(1 <= T <= 10), indicating the number of test cases.

Each test case contains several lines.

The first line contains an integer N(2 <= N <= 105), representing the number of cities.

Then the followingN-1 lines each contains three integersX, Y,Z(1 <= X, Y <= N, 1 <= Z <= 106), means that there is a highway between city X and city Y , and length of that highway.

You can assume all the cities are connected and the highways are bi-directional.

Output

For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y represents the largest total travel distance of all people.

Sample Input

2
4
1 2 3
2 3 2
4 3 2
6
1 2 3
2 3 4
2 4 1
4 5 8
5 6 5

Sample Output

Case #1: 18
Case #2: 62

题意:一颗树。相应1-n的结点,如今要求是每一个结点原本的人都走到不一样的结点去,每一个人都有路程,求总的最大路程

思路:对于每条边我们能想到的是:这条边左边的结点都跑到右边去,右边的 结点都跑到左边去。所以每条边。都会被走min{left[num], sum-left[nu,]}*2次,依据这个原则我们进行树形DP。可是这题递归的写法会跪。所以仅仅能手动DFS

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 200010;

struct Node {
	int to, next;
	int len;
} edge[maxn<<1];
int head[maxn], num[maxn], cnt, n;
int sta[maxn], vis[maxn];
ll ans;

void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
	memset(num, 0, sizeof(num));
}

void add(int u, int v, int len) {
	edge[cnt].to = v;
	edge[cnt].len = len;
	edge[cnt].next = head[u];
	head[u] = cnt++;

	edge[cnt].to = u;
	edge[cnt].len = len;
	edge[cnt].next = head[v];
	head[v] = cnt++;
}

void dfs(int u) {
	memset(vis, 0, sizeof(vis));
	int top = 0;
	sta[top++] = u;
	vis[u] = 1;
	while (top > 0) {
		int flag = 1;
		int cur = sta[top-1];
		for (int i = head[cur]; i != -1; i = edge[i].next) {
			int v = edge[i].to;
			if (vis[v]) continue;
			flag = 0;
			sta[top++] = v;
			vis[v] = 1;
		}

		if (flag == 0) continue;
		top--;

		for (int i = head[cur]; i != -1; i = edge[i].next) {
			int v = edge[i].to;
			if (num[v] != 0) {
				num[cur] += num[v];
				ans += (ll) edge[i].len * min(num[v], n - num[v]);
			}
		}
		num[cur]++;
	}
}

int main() {
	int t, cas = 1;
	int u, v, w;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		init();
		ans = 0;
		for (int i = 1; i < n; i++) {
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w);
		}
		dfs(1);
		printf("Case #%d: %I64d\n", cas++, ans*2);
	}
	return 0;
}

HDU - 4118 Holiday's Accommodation

时间: 2024-08-24 13:10:54

HDU - 4118 Holiday&#39;s Accommodation的相关文章

HDU 4118 - Holiday&#39;s Accommodation

Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Java/Others)Total Submission(s): 2938    Accepted Submission(s): 902 Problem Description Nowadays, people have many ways to save money on accommodation w

hdu 4118 Holiday&#39;s Accommodation 树形dp

Holiday's Accommodation Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4118 Description Nowadays, people have many ways to save money on accommodation when they are on vacation.One of these ways is exchanging

hdu 4118 dfs

题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值.不能重复 Sample Input 1 4 1 2 3 2 3 2 4 3 2 Sample Output Case #1: 18 //1去4,4去1,2去3,3去2 思路:对于每一个边,都可以把这个变量偷的点交换,这样这些点都要走这个边,就是这个边要走2*min(左端的点数,右端的点数).但是会爆栈,可以人工开栈,也可用非递归的方法写dfs,同样可以过. hdu用c++交,g++会爆栈 1 #pr

hdu 5348 MZL&amp;#39;s endless loop

给一个无向图(事实上是有向的.可是没有指定边的方向),你须要指定边的方向,使得每一个点入度和出度相差不超过1. 事实上就是找很多条路径.合起来能走完这个图..先统计各个顶点的度.度为奇数必是起点或终点,否则是中间点或者同为起点和终点. 邻接表建图(建双向),先从每一个奇数度顶点出发找路径,再从偶数度顶点出发找路径.经过的边要删去不然超时. #include <iostream> #include <cstring> #include <cstdio> #include

hdu 4274 Spy&amp;#39;s Work(水题)

Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1266    Accepted Submission(s): 388 Problem Description I'm a manager of a large trading company, called ACM, and responsible for the

HDU 1160 FatMouse&amp;#39;s Speed DP题解

本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,只是由于须要按原来的标号输出,故此须要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法.主要的一维动态规划法了. 记录路径:仅仅须要记录后继标号,就能够逐个输出了. #include <stdio.h> #include <algorithm> using namespace std; const int MAX_N = 1005; struct MouseSpeed { int id, w, s;

hdu 4122 Alice&amp;#39;s mooncake shop (线段树)

题目大意: 一个月饼店每一个小时做出月饼的花费不一样. 储存起来要钱.最多存多久.问你把全部订单做完的最少花费. 思路分析: ans = segma( num[]*(cost[] + (i-j)*s) ) 整理一下会发现式子就是 cost[]-j*s + i*s 对于每个订单,我们把i拿出来分析 所以也就用cost - j*s 建树. 然后在储存期间找到最小的花费即可了. #include <cstdio> #include <iostream> #include <algo

HDU 3966 Aragorn&amp;#39;s Story(树链剖分)

HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 50005; inline int lowbit(int x) {return x&(-x);} int dep

hdu 1250 Hat&amp;#39;s Fibonacci

点击此处就可以传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) You