zoukankan      html  css  js  c++  java
  • HDU 4118 Holiday's Accommodation(树形DP)

    Holiday's Accommodation

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


    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
     
    Source
     
    Recommend
    chenyongfu   |   We have carefully selected several similar problems for you:  4119 4112 4114 4115 4111 
     
    /*
    题意:n个结点,每个节点都有一个房子和一个人,这些人都想环游世界,也就是所有结点都去一遍,他们旅游的时候会选则最近的路线,
    让你求所有人都达成愿望之后最多的路程
    
    思路:这个题光是题意就看了很长时间,刚开始理解成一个人只要到达一个城市就好了,显然理解偏了。
    每一条路最多能给总路程提供的价值:很显然就是这条路两边的相对较小的定点数×这条路的长的,然后dfs搜一遍就好了
    */
    #include<stdio.h>
    #include<vector>
    #include<string.h>
    #include<iostream>
    #define N 100005
    using namespace std;
    struct node
    {
        int to,len;//下一个节点是哪里,以to为重点的边多长
        node(int x=0,int y=0)
        {
            to=x;len=y;
        }
    }fr[N];
    vector<node > v[N];//构图用的数组
    int t,n;
    long long sum;
    long long dp[N];//表示到i点位置,左边子树有多少个点
    bool visit[N];//记录这个点走没走
    void dfs(int id,int len)
    {
        //cout<<"id="<<id<<endl;
        visit[id]=true;
        //cout<<dp[id]<<endl;
        for(int i=0;i<v[id].size();i++)
        {
            int next=v[id][i].to;//下一步要走的路;
            int next_len=v[id][i].len;//下一条路的长度
            if(visit[next]) continue;//这一步走了就跳过
            dfs(next,next_len);
            dp[id]+=dp[next];
        }
        dp[id]++;
        //cout<<"dp["<<id<<"]="<<dp[id]<<endl;
        sum+=(long long)min(dp[id],n-dp[id])*len;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%d",&t);
        for(int l=1;l<=t;l++)
        {
            sum=0;
            memset(dp,0,sizeof dp);
            memset(visit,false,sizeof visit);
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                v[i].clear();
            for(int i=1;i<n;i++)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                //cout<<a<<" "<<b<<" "<<c<<endl;
                v[a].push_back(node(b,c));
                v[b].push_back(node(a,c));
            }    
            dfs(1,0);
            printf("Case #%d: %lld
    ",l,sum*2);
        }
        return 0;
    }
  • 相关阅读:
    传输线
    互连设计
    数字IC·功耗
    [z]一个合格的FPGA工程师需要掌握哪些知识?
    Arduino I2C + DS1307实时时钟
    Arduino I2C + 温湿度传感器Si7021
    Arduino I2C + 温湿度传感器AM2321
    Arduino I2C + 数字式环境光传感器BH1750FVI
    什么是“光照度(Illuminance)”?
    Arduino ADC + 模拟温度传感器LM35D
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5788140.html
Copyright © 2011-2022 走看看