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;
}






posted @ 2016-04-20 18:31  mengfanrong  阅读(235)  评论(0编辑  收藏  举报