(树形DP) hdu 4118

Holiday's Accommodation

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Java/Others)
Total Submission(s): 2665    Accepted Submission(s): 757


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
 
题意:
 
每个点都有一个人,每个人都不能在原来的位置,求交换后走的最大值
 
树形DP啊,
 
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<map>
using namespace std;
vector<int> e[100005],w[100005];
long long ans;
int n;
int son[100005];
void dfs(int u,int father)
{
    son[u]=1;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v==father)
            continue;
        dfs(v,u);
        son[u]+=son[v];
        int num=min(son[v],n-son[v]);
        ans+=(long long)(num*w[u][i]*2);
    }
}
int main()
{
    int tt,cas=1;
    scanf("%d",&tt);
    while(tt--)
    {
        memset(son,0,sizeof(son));
        scanf("%d",&n);
        ans=0;
        for(int i=1;i<=n;i++)
            e[i].clear(),w[i].clear();
        for(int i=1;i<n;i++)
        {
            int x,y,z;
            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);
        }
        dfs(1,-1);
        printf("Case #%d: %I64d\n",cas++,ans);
    }
    return 0;
}

  

posted @ 2015-05-13 22:29  waterfull  阅读(127)  评论(0编辑  收藏  举报