hdu-4118 Holiday's Accommodation(树形dp+树的重心)
题目链接:
Holiday's Accommodation
Time Limit: 8000/4000 MS (Java/Others)
Memory Limit: 200000/200000 K (Java/Others)
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.
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+;
const int maxn=; int n,son[N],num,head[N],cnt;
LL dis[N],ans,sum;
struct Edge
{
int to,next,val;
}edge[*N];
void addedge(int s,int e,int va)
{
edge[cnt].to=e;
edge[cnt].next=head[s];
edge[cnt].val=va;
head[s]=cnt++;
}
void dfs(int x,int fa)
{
int mmax=;
sum=sum+dis[x];
son[x]=;
for(int i=head[x];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(y==fa)continue;
dis[y]=dis[x]+edge[i].val;
dfs(y,x);
son[x]+=son[y];
}
}
void dfs1(int x,int fa,LL dist)
{
ans=min(ans,dist);
for(int i=head[x];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(y==fa)continue;
LL s=dist+(LL)(n-*son[y])*(LL)edge[i].val;
dfs1(y,x,s);
}
} int main()
{
int t;
read(t);
int Case=;
while(t--)
{
read(n);
int x,y,z;
cnt=;
ans=inf;
sum=;
mst(head,-);
for(int i=;i<n;i++)
{
read(x);read(y);read(z);
addedge(x,y,z);
addedge(y,x,z);
}
dis[]=;
dfs(,-);
dfs1(,-,sum);
printf("Case #%d: %lld\n",Case++,*ans);
}
return ;
}
hdu-4118 Holiday's Accommodation(树形dp+树的重心)的更多相关文章
- HDU 4118 Holiday's Accommodation(树形DP)
Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Jav ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- HDU 4118 Holiday's Accommodation
Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Jav ...
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- POJ3107Godfather[树形DP 树的重心]
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6121 Accepted: 2164 Descrip ...
- 树形dp&&树的重心(D - Godfather POJ - 3107)
题目链接:https://cn.vjudge.net/contest/277955#problem/D 题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小). ...
- POJ 2378.Tree Cutting 树形dp 树的重心
Tree Cutting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4834 Accepted: 2958 Desc ...
- poj1655(dfs,树形dp,树的重心)(点分治基础)
题意:就是裸的求树的重心. #include<cstring> #include<algorithm> #include<cmath> #include<cs ...
- HDU 4118 Holiday's Accommodation (dfs)
题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值. 析:对于每边条,我们可以这么考虑,它的左右两边的点数最少的就是要加的数目,因为最好的情况就是左边到右 ...
随机推荐
- 72.spring boot讨论群【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 如果您碰到什么问题,您可以加群进行探讨,在群里有加入的都是Spring Boot志同道合的朋友: Spring Boot QQ交流群:193341 ...
- nginx1.6.3
Nginx1.6.3安装配置 安装时关闭防火墙和selinuxservice iptables stopsed -i "s/selinux=enabled/selinux=disable/g ...
- 字典树模板题 POJ 2503
#include <cstdio> #include <cstring> ],fr[]; int st; struct Tire{ ]; ]; }node[]; void in ...
- hdu 2126背包问题
/*有n件物品,旅客一共有m块大洋.第一个问题,旅客最多可以买多少件物品?请注意,这里是多少件,不是价值最大.所以这个非常好求,将所有的物品按照价值排序,先买便宜的,再买贵的.贪心的思想.这个地方有些 ...
- cp: omitting directory解决方案
cp: omitting directory是因为目录下面还有目录.应该使用递归方法.需要加入-r参数. 及:cp -r 该目录名.
- 【BZOJ1430】小猴打架(Prufer编码)
题意:求n个点带编号生成树的不同加边序列个数 n<=10^6 思路: WJMZBMR:额.首先他们打架的关系是一颗无根树,就有n^(n-2)种情况,还有打架的顺序,是(n-1)!种,乘起来就可以 ...
- BZOJ1986: [USACO2004 Dec] Dividing the Path 划区灌溉
L<=1000000的土地上用长度在2*A~2*B的线段覆盖所有点,且给定n<=1000个区间,每个区间上只允许有一条线段,求最少多少线段,无解-1. f[i]表示填前i个土地最少线段,f ...
- <转>C#中线程的学习
原文发布时间为:2008-11-15 -- 来源于本人的百度文章 [由搬家工具导入] http://hi.baidu.com/cyap/blog/category/%B6%E0%CF%DF%B3%CC ...
- java多线程调试
1. 多线程调试 https://blog.csdn.net/bramzhu/article/details/52367052 https://www.jb51.net/article/129632. ...
- python学习之-- 协程
协程(coroutine)也叫:微线程,是一种用户态的轻量级线程,就是在单线程下实现并发的效果.优点:1:无需线程上下文切换的开销.(就是函数之间来回切换)2:无需原子操作锁定及同步的开销.(如改一个 ...