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= ;
struct Edge{
int v ;
int next ;
LL w ;
};
Edge edge[Max_N*] ;
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= ;
for(int e=vec[u];e!=-;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=;ca<=T;ca++){
scanf("%d",&N) ;
id= ;
fill(vec,vec++N,-) ;
for(int i=;i<N;i++){
scanf("%d%d%d",&u,&v,&w) ;
add_edge(u,v,w) ;
add_edge(v,u,w) ;
}
sum= ;
dfs(,-) ;
printf("Case #%d: ",ca) ;
cout<<sum<<endl ;
}
return ;
}

HDU 4118 Holiday's Accommodation的更多相关文章

  1. HDU 4118 Holiday's Accommodation(树形DP)

    Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Jav ...

  2. HDU 4118 Holiday's Accommodation (dfs)

    题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值. 析:对于每边条,我们可以这么考虑,它的左右两边的点数最少的就是要加的数目,因为最好的情况就是左边到右 ...

  3. HDU - 4118 Holiday&#39;s Accommodation

    Problem Description Nowadays, people have many ways to save money on accommodation when they are on ...

  4. HDU 4118 树形DP Holiday's Accommodation

    题目链接:  HDU 4118 Holiday's Accommodation 分析: 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍. 代码: #include<iostrea ...

  5. hdu-4118 Holiday's Accommodation(树形dp+树的重心)

    题目链接: Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 200000/200000 ...

  6. hdu 4118 dfs

    题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值.不能重复 这题的方法很有看点啊,标记为巩固题 Sample Input 1 4 1 2 3 2 3 2 ...

  7. 树形DP(Holiday's Accommodation HDU4118)

    题意:有n间房子,之间有n-1条道路连接,每个房间里住着一个人,这n个人都想到其他房间居住,并且每个房间不能有两个人,问所有人的路径之和最大是多少? 分析:对于每条边来说,经过改边的人由该边两端元素个 ...

  8. hdu 4118 树形dp

    思路:其实就是让每一条路有尽量多的人走. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<m ...

  9. [GodLove]Wine93 Tarining Round #4

    比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44903#overview 题目来源: 2011 Asia ChengDu R ...

随机推荐

  1. astats日志分析系统

    Awstats是一个免费非常简洁而且强大有个性的网站日志分析工具. 功能: 一:访问量,访问次数,页面浏览量,点击数,数据流量等 二:精确到每月.每日.每小时的数据 三:访问者国家 四:访问者IP 五 ...

  2. Eclipse的中文字体设置

    打开eclipse中文字体很小,简直难以辨认.在网上搜索发现这是由于Eclipse 用的字体是 Consolas,显示中文的时候默认太小了.解决方式有两种:一.把字体设置为Courier New  操 ...

  3. 【linux】umask

    1.默认情况下的umask值是022 [root@andon ~]# umask 0022  2.umask作用 默认文件权限:666(linux创建文件默认无执行权限)-umask =644(6-0 ...

  4. POM (Project Object Model)简介

    1  概念介绍 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它使用 java1.5 来编译. < ...

  5. gcc和ld 中的参数 --whole-archive 和 --no-whole-archive

    首先 --whole-archive 和 --no-whole-archive 是ld专有的命令行参数,gcc 并不认识,要通gcc传递到 ld,需要在他们前面加 -Wl,字串. --whole-ar ...

  6. datagridview 不显示行号的问题

    环境:C#,Winform 场景: 窗体上有两个tab页A.B,每个tab页上都有一个DatagridView.窗体加载后,显示tab A选项卡.序号正常显示,但点击B选项卡后,DatagridVie ...

  7. Ubuntu Server上的LVM配置

    在安装Linux的时候,通常遇到的一个比较头痛的问题就是分区,到底每个区该分多少,用了一段时间之后,某个分区又不够用了,该怎么办?如果是普通的服务器,那一切都好说,大不了就关机重新划分分区嘛,但是对于 ...

  8. try catch 怎么写?

    除非必要,否则不在底层写try catch. 比如说,需要在catch里做一些处理,然后再抛出,一般不建议使用try catch掩盖程序出现的异常. try {     BuildQueryComma ...

  9. CF 501C Misha and Forest 好题

    C. Misha and Forest   Let's define a forest as a non-directed acyclic graph (also without loops and ...

  10. POJ 1155 TELE 背包型树形DP 经典题

    由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...