zoukankan      html  css  js  c++  java
  • HDU 4118

    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 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
     
    题意:
      给出一颗树,树上有N个点,每一个点都有一个人,求所有人不在原位置所经过的最大距离,
    思路:
      从每条边去想,一条边可以贡献的距离最大为多少
    对于一条边,左右分为两个子树,所以贡献值最大为 两边人数最小的个数 * 边的值
     
    AC代码:
     1 # include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAX = 100010;
     4 typedef long long int ll;
     5 struct Node
     6 {
     7     int to;
     8     int next;
     9     ll val;
    10 }tree[MAX * 2];
    11 int head[MAX];
    12 int tol = 0;
    13 int num[MAX * 2];
    14 ll sum = 0;
    15 int n;
    16 void add(int a, int b, ll val)
    17 {
    18     tree[tol].to = b;
    19     tree[tol].next = head[a];
    20     tree[tol].val = val;
    21     head[a] = tol++;
    22 }
    23 void dfs(int root, int f)
    24 {
    25     num[root] = 1;
    26     for(int i = head[root]; i != -1; i = tree[i].next)
    27     {
    28         int son = tree[i].to;
    29         if(son == f)
    30             continue;
    31         dfs(son, root);
    32         
    33         num[root] += num[son];
    34         ll tep = min(num[son], n - num[son]);
    35         sum += tep * (tree[i].val) * 2LL;
    36     }
    37 }
    38 int main()
    39 {
    40     int t;
    41     scanf("%d", &t);
    42     int case_ = 0;
    43     while(t--)
    44     {
    45         memset(head, -1, sizeof(head));
    46         memset(num, 0, sizeof(num));
    47         tol = 0;
    48         sum = 0;
    49         scanf("%d", &n);
    50         int a, b;
    51         ll val;
    52         for(int i = 1; i < n; i++)
    53         {
    54             scanf("%d%d%I64d", &a, &b, &val);
    55             add(a, b, val);
    56             add(b, a, val);
    57         }
    58         dfs(1, -1);
    59         printf("Case #%d: %I64d
    ", ++case_, sum);
    60     }
    61     return 0;
    62 }
    View Code
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    MySQL存储过程和函数
    MySQL数据类型
    MySQL—基础(SQL语句)
    如何将一串字符串按照某个特定的字符分割后倒叙输出,如:www.baidu.com输出为com.baidu.www
    JAVA WEB数据中文编码问题
    如何用一条SQL语句从登录日志表中查询统计出每个人登录的次数
    JAVA WEB tomcat启动关闭问题
    thinkphp知识点
    smarty模板内容
    smarty基础
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5816056.html
Copyright © 2011-2022 走看看