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=,int y=)
{
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=;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=;l<=t;l++)
{
sum=;
memset(dp,,sizeof dp);
memset(visit,false,sizeof visit);
scanf("%d",&n);
for(int i=;i<=n;i++)
v[i].clear();
for(int i=;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(,);
printf("Case #%d: %lld\n",l,sum*);
}
return ;
}

HDU 4118 Holiday's Accommodation(树形DP)的更多相关文章

  1. HDU 4118 Holiday's Accommodation

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

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

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

  3. HDU 4118 Holiday's Accommodation (dfs)

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

  4. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  5. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

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

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

  7. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  8. hdu 4003 Find Metal Mineral 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...

  9. HDU 5758 Explorer Bo(树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次 ...

随机推荐

  1. Python 接口测试(五)

    五:使用python进行组织编写接口测试用例 接口测试其实就是几个步骤. 拿到接口的url地址 查看接口是用什么方式发送 添加请求头,请求体 发送查看返回结果,校验返回结果是否正确 明白了接口测试的测 ...

  2. ioc(Inversion of Control)控制反转和DI

    ioc意味着将你设计好的交给容器控制,而不是传统在你的对象中直接控制 谁控制了谁:传统的javaSE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象:而ioc是有专门一个容 ...

  3. 我是如何利用Hadoop做大规模日志压缩的

    背景 刚毕业那几年有幸进入了当时非常热门的某社交网站,在数据平台部从事大数据开发相关的工作.从日志收集.存储.数据仓库建设.数据统计.数据展示都接触了一遍,比较早的赶上了大数据热这波浪潮.虽然今天的人 ...

  4. Linux - 设置/取消代理

    export http_proxy=118.210.42.251:44367 或: export https_proxy=118.210.42.251:44367   要取消该设置: unset ht ...

  5. WebApi实现原理解析笔记

    这是我看过WebApi实现代码后的一些总结,一方面加深自己的记忆,另外也希望能够帮助大家更深入的了解WebApi. 注:暂时没有好好的整理,可能有些晦涩难懂. Webapi 控制器类必须实现IHttp ...

  6. WaitAll 和 WhenAll 的使用及区别

    用过.net 异步编程的同学都知道,比以前的多线程编程实现起来真的方便很多,今天把WaitAll和WhenAll这两种编程方式回顾总结一下(当然WaitAny.WhenAny是一样的操作) 1:Wai ...

  7. 【转】常用Maven插件

    我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...

  8. Cow Uncle 学习了叉积的一点运用,叉积真的不错

    Cow Uncle Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...

  9. poj3264 最大值与最小值的差

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  10. SQL Server 后悔药 delete drop update

    国庆假期终于有时间做点事情 因为平常工作会做些数据库操作 可能会有所操作失误  参考一下 方法一 ApexSql 2016一个软件 http://www.cnblogs.com/gsyifan/p/A ...