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条有权值的边,求所有人不在原来位置所移动的距离的和最大值. 析:对于每边条,我们可以这么考虑,它的左右两边的点数最少的就是要加的数目,因为最好的情况就是左边到右 ...
随机推荐
- 图的最小生成树——Kruskal算法
Kruskal算法 图的最小生成树的算法之一,运用并查集思想来求出最小生成树. 基本思路就是把所有边从小到大排序,依次遍历这些边.如果这条边所连接的两个点在一个连通块里,遍历下一条边,如果不在,就把这 ...
- COJ 1208 矩阵快速幂DP
题目大意: f(i) 是一个斐波那契数列 , 求sum(f(i)^k)的总和 由于n极大,所以考虑矩阵快速幂加速 我们要求解最后的sum[n] 首先我们需要思考 sum[n] = sum[n-1] + ...
- Hibernate 批处理(batch inserts, updates and deletes)
总结:hibernate在进行批量处理不给力的主要原因就是Session中存在缓存,而hibernate的机制就是通过session中的一级缓存去同步数据库,所以当进行批量处理时,缓存中保存的数据量很 ...
- IIS文件存在但报404问题解决
遇到一个奇怪的问题,在IIS7.5中,一些样式和JS文件存在,但访问就是报404. 根据网上搜索到的解决方法,发现解决不了,不同同样的问题引起的. 网上解决: 1.没有配置合适的MIME信息,通过添加 ...
- 2016 Multi-University Training Contest 7 solutions BY SYSU
Ants 首先求出每个点的最近点. 可以直接对所有点构造kd树,然后在kd树上查询除本身以外的最近点,因为对所有点都求一次,所以不用担心退化. 也可以用分治做,同样是O(NlogN)的复杂度. 方法是 ...
- Hihocoder #1067 : 最近公共祖先·二
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个人的所有共同祖先中 ...
- iOS - 设置系统类似的方法弃用警告的方式
在开发过程中,调用系统方法时,经常可以看xCode 提示 该方法已弃用,如下图: 觉得特别炫,查一下资料,如果自己也想实现如下的效果,只需要采用系统的如下几个关键字加在方法名后面就可以了: NS_DE ...
- Permutation Sequence(超时,排列问题)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- scoi2018游记
day1: t1点分树 冬令营上jry讲过原题,t2启发式合并+解二次同余方程 预计100+100+0 结果t1卡内存,t2模数太大.导致调试到没有写t3 最后t1 85 t2 15 要是我会o1快速 ...
- TDBXJSONStream(BERLIN新增)的使用
DELPHI 10.1 BERLIN新增TDBXJSONStream类,用于方便地将数据序列为JSON,和将JSON还原出来数据. DATASNAP远程方法也相应地增加了支持返回TDBXJSONStr ...