zoukankan      html  css  js  c++  java
  • HDU 4118 Holiday's Accommodation

    Holiday's Accommodation

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


    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
    #include <iostream>
    #include <stdio.h>
    #include <queue>
    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <queue>
    #include <set>
    #include <algorithm>
    #include <map>
    #include <stack>
    #include <math.h>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #pragma comment(linker, "/STACK:10240000000000,10240000000000")
    using namespace std;
    typedef long long LL ;
    const int Max_N=100008 ;
    struct Edge{
        int v ;
        int next ;
        LL w ;
    };
    Edge edge[Max_N*2] ;
    int vec[Max_N] ,id ,N;
    inline void add_edge(int u ,int v ,int w){
        edge[id].v=v ;
        edge[id].w=w ;
        edge[id].next=vec[u] ;
        vec[u]=id++ ;
    }
    LL sum ;
    int dfs(int u ,int father){
        int son=1  ;
        for(int e=vec[u];e!=-1;e=edge[e].next){
            int v=edge[e].v ;
            LL w=edge[e].w ;
            if(v!=father){
                int v_son=dfs(v,u) ;
                sum=sum+w*Min(N-v_son,v_son)*2LL ;
                son+=v_son ;
            }
        }
        return son ;
    }
    int main(){
       int T ,u , v , w ;
       scanf("%d",&T)  ;
       for(int ca=1;ca<=T;ca++){
           scanf("%d",&N) ;
           id=0 ;
           fill(vec,vec+1+N,-1) ;
           for(int i=1;i<N;i++){
               scanf("%d%d%d",&u,&v,&w) ;
               add_edge(u,v,w) ;
               add_edge(v,u,w) ;
           }
           sum=0 ;
           dfs(1,-1) ;
           printf("Case #%d: ",ca) ;
           cout<<sum<<endl ;
       }
       return 0 ;
    }
  • 相关阅读:
    作品-系统-[原生开发]新蓝景CRM管理系统
    作品-网站-[二次开发]汇名惠商城
    js声明json数据,打印json数据,遍历json数据,转换json数据为数组
    js 鼠标滚动到某屏时,加载那一屏的数据,仿京东首页楼层异步加载模式
    js转换数据类型为浮点型,并取两位小数点
    php判断是否是微信客户端的浏览器访问
    百度地图API地理位置和坐标转换
    php 对象中连贯执行方法
    php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容
    php 获取远程图片保存到本地
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3388733.html
Copyright © 2011-2022 走看看