Problem Descriptionnowadays, people has many ways to save money in accommodation when they is on vacation.
One of these ways is exchanging houses and other people.
Here are 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 of that choose a destination for each person. There is 2 rules should be satisfied:
1. All the people should go to one of the other people's City.
2. The them never go to the same city, because they is not willing to share a house.
They want to maximize the sum of all people ' s travel distance. The travel distance of a person are 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's your job to find the best plan, which maximum the total travel distance of all PEOPL E.
Inputthe first line of input contains one integer t (1 <= t <=), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer n (2 <= N <=), 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 ther E is a highway between city X and City Y, and the length of that highway.
You can assume all the cities is connected and the highways are bi-directional.
Outputfor 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
241 2 32 3 24 3 261 2 32 3 42 4 14 5 85 6 5
Sample Output
Case #1:18Case #2:62
Test instructions: a tree. The corresponding 1-n node, now requires that each node is the original person to go to a different node to go, everyone has the journey, to seek the total maximum distance
Idea: For each side we can think of is: this side of the left of the node to go to the right side, the right side of the node all run to the left. So each side. will be gone min{left[num], sum-left[nu,]}*2 times, according to this principle we carry out the tree-shaped DP. But the recursive wording of this question will kneel. So just be able to manually 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], n UM[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].t o = 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 CU r = 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[to p++] = 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);p rintf ("Case #%d:%i64d\n", cas++, ans*2);} return 0;}
HDU-4118 holiday& #39; s accommodation