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
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.
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.
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
Case #2: 62
#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的更多相关文章
- HDU 4118 Holiday's Accommodation(树形DP)
Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Jav ...
- HDU 4118 Holiday's Accommodation (dfs)
题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值. 析:对于每边条,我们可以这么考虑,它的左右两边的点数最少的就是要加的数目,因为最好的情况就是左边到右 ...
- HDU - 4118 Holiday's Accommodation
Problem Description Nowadays, people have many ways to save money on accommodation when they are on ...
- HDU 4118 树形DP Holiday's Accommodation
题目链接: HDU 4118 Holiday's Accommodation 分析: 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍. 代码: #include<iostrea ...
- hdu-4118 Holiday's Accommodation(树形dp+树的重心)
题目链接: Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 ...
- hdu 4118 dfs
题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值.不能重复 这题的方法很有看点啊,标记为巩固题 Sample Input 1 4 1 2 3 2 3 2 ...
- 树形DP(Holiday's Accommodation HDU4118)
题意:有n间房子,之间有n-1条道路连接,每个房间里住着一个人,这n个人都想到其他房间居住,并且每个房间不能有两个人,问所有人的路径之和最大是多少? 分析:对于每条边来说,经过改边的人由该边两端元素个 ...
- hdu 4118 树形dp
思路:其实就是让每一条路有尽量多的人走. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<m ...
- [GodLove]Wine93 Tarining Round #4
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44903#overview 题目来源: 2011 Asia ChengDu R ...
随机推荐
- HDP2.4安装(三):MySql安装
在安装Ambari时,Ambari默认的数据库是ProstgreSQL,对ProstgreSQL不太熟悉,选择使用MySql. 但Centos 7 默认支持的是MariaDB数据库. MariaDB是 ...
- Android MVC模式
Android MVC模式 下面是我对Android MVC模式的理解 Model 模型层 包括实体模型层,存放程序中调用的实体. 业务模型层,存放程序中调用的业务逻辑. View 显示层 An ...
- (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)
本文转载自:http://blog.csdn.net/baimafujinji/article/details/38026421 Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其 ...
- TX Textcontrol 使用总结三——禁用右键、模版合并
一.Tx Textcontrol如何禁用右键快捷菜单? ==> 添加txContent_TextContextMenuOpening事件,实现方式如下所示: private void txCon ...
- mongodb3.03开启认证
原文地址:http://21jhf.iteye.com/blog/2216103 下载了最新mongodb3.03版本,当使用--auth 参数命令行开启mongodb用户认证时遇到很多问题,现总结如 ...
- 单选按钮选中js的处理
function FinancialinfosetController($scope, $http, $timeout, $location, $rootScope, $routeParams) { ...
- C++命名空间 namespace的作用和使用解析
一. 为什么需要命名空间(问题提出) 命名空间是ANSIC++引入的可以由用户命名的作用域,用来处理程序中 常见的同名冲突. 在 C语言中定义了3个层次的作用域,即文件(编译单元).函数和复合语句.C ...
- eclipse升级,导入旧版eclipse的插件[转]
启动 eclipse.菜单 File => import … => Install => From existing Installation, 点确定, 就会弹出对话框, 浏览选择 ...
- AOP 下的权限控制实现
摘要 面向方面的编程(AOP)是一种新的编程技术,它弥补了面向对象的编程(OOP)在跨越模块行为上的不足.AOP 引进了 Aspect,它将影响多个类的行为封装到一个可重用模块中,它允许程序员对横切 ...
- java反射机制详解 及 Method.invoke解释
JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为ja ...