hdu-4118 Holiday's Accommodation(树形dp+树的重心)
题目链接:
Holiday's Accommodation
Time Limit: 8000/4000 MS (Java/Others)
Memory Limit: 200000/200000 K (Java/Others)
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.
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.
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
题意:
给出一个无向图,现在把所有的点的人都交换,保证每个地方只有一个人,且任何人都不在自己原来的那个点上,问交换的过程中所有人走的最远的距离是多少;
思路:
走的距离和最大,那么就是求树的重心,再求二倍的所有点到重心的距离和;
树的重心的性质就是树上所有点到重心的距离和是最小的;
AC代码:
//#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<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=1e5+10; const int maxn=1005; int n,son[N],num,head[N],cnt; LL dis[N],ans,sum; struct Edge { int to,next,val; }edge[2*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=0; sum=sum+dis[x]; son[x]=1; for(int i=head[x];i!=-1;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!=-1;i=edge[i].next) { int y=edge[i].to; if(y==fa)continue; LL s=dist+(LL)(n-2*son[y])*(LL)edge[i].val; dfs1(y,x,s); } } int main() { int t; read(t); int Case=1; while(t--) { read(n); int x,y,z; cnt=0; ans=inf; sum=0; mst(head,-1); for(int i=1;i<n;i++) { read(x);read(y);read(z); addedge(x,y,z); addedge(y,x,z); } dis[1]=0; dfs(1,-1); dfs1(1,-1,sum); printf("Case #%d: %lld\n",Case++,2*ans); } return 0; }