HDU4118:Holiday's Accommodation(思维 & dfs)

来源:互联网 发布:妙笔生花软件安卓 编辑:程序博客网 时间:2024/06/05 11:09

Holiday's Accommodation

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


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
241 2 32 3 24 3 261 2 32 3 42 4 14 5 85 6 5
 

Sample Output
Case #1: 18Case #2: 62
 

Source
2011 Asia ChengDu Regional Contest
题意:N个节点的树,每个点有一个人,要求每个人去到其他节点,且每个节点只能容纳一个人,求总的路程的最大值。

思路:假如有边1-2-3-4,对于2和3之间的边,最多能走四遍,即2和3的较小子树节点数*2,就是2和3两边的人全部交换了,那么对于每条边是否都能达到其最大值呢?答案是可以,只要围着重心对称交换人就行了,那么这题就相当于求节点的子树点数了,怎么花式求解都可以。

# include <iostream># include <cstdio># include <cstring># include <algorithm>using namespace std;typedef long long LL;const int maxn = 1e5+3;struct node{int e, w, next;}edge[maxn<<1];int n, cnt, id, Next[maxn], in[maxn], out[maxn];LL ans;void add_edge(int u, int v, int w){    edge[cnt] = {v, w, Next[u]};    Next[u] = cnt++;    edge[cnt] = {u, w, Next[v]};    Next[v] = cnt++;}void dfs(int cur, int pre, int w){    in[cur] = ++id;    for(int i=Next[cur]; i!=-1; i=edge[i].next)    {        int v = edge[i].e;        if(v == pre) continue;        dfs(v, cur, edge[i].w);    }    out[cur] = id;    int imin = min(out[cur]-in[cur]+1, n-out[cur]+in[cur]-1);    ans += (LL)imin*2*w;}int main(){    int t, a, b, c, cas=1;    scanf("%d",&t);    while(t--)    {        cnt = id = ans = 0;        memset(Next, -1, sizeof(Next));        scanf("%d",&n);        for(int i=1; i<n; ++i)        {            scanf("%d%d%d",&a,&b,&c);            add_edge(a, b, c);        }        dfs(1, 0, 0);        printf("Case #%d: %lld\n",cas++, ans);    }    return 0;}



原创粉丝点击