HDOJ 4118 Holiday's Accommodation
来源:互联网 发布:hanoi塔c语言递归详解 编辑:程序博客网 时间:2024/06/11 13:57
每条边要走的次数刚好的是这条边两端的点数的最小值的两倍。
Holiday's Accommodation
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Java/Others)Total Submission(s): 2116 Accepted Submission(s): 591
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
241 2 32 3 24 3 261 2 32 3 42 4 14 5 85 6 5
Sample Output
Case #1: 18Case #2: 62
Source
2011 Asia ChengDu Regional Contest
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <map>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;typedef long long int LL;typedef pair<int,int> pII;struct Edge{ int to,next,weight;}edge[200200];int Adj[100100],Size,n;map<pII,int> vis;void init(){ Size=0; memset(Adj,-1,sizeof(Adj));}void Add_Edge(int u,int v,int c){ edge[Size].to=v; edge[Size].next=Adj[u]; edge[Size].weight=c; Adj[u]=Size++;}LL dfs(int f,int u){ if(vis.count(make_pair(f,u))) return vis[make_pair(f,u)]; vis[make_pair(f,u)]=1; for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(v==f) continue; vis[make_pair(f,u)]+=dfs(u,v); } return vis[make_pair(f,u)];}int getvalu(int a,int b){ if(vis[make_pair(a,b)]) return vis[make_pair(a,b)]; return n-vis[make_pair(b,a)];}LL doit(){ for(int i=Adj[1];~i;i=edge[i].next) { int v=edge[i].to; if(!vis.count(make_pair(1,v))) dfs(1,v); } LL ret=0; for(int i=1;i<=n;i++) { for(int j=Adj[i];~j;j=edge[j].next) { int u=edge[j].to; ret+=(LL)edge[j].weight*min(getvalu(i,u),getvalu(u,i)); } } return ret;}int main(){ int T_T,cas=1; scanf("%d",&T_T);while(T_T--){ scanf("%d",&n); init(); vis.clear(); for(int i=0;i<n-1;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); Add_Edge(a,b,c); Add_Edge(b,a,c); } printf("Case #%d: %I64d\n",cas++,doit());} return 0;}
0 0
- HDOJ 4118 Holiday's Accommodation
- HDU 4118 Holiday's Accommodation
- HDU-4118-Holiday's Accommodation
- hdu 4118 Holiday's Accommodation
- HDU - 4118 Holiday's Accommodation
- 【树形DP】 HDU 4118 Holiday's Accommodation
- hdu 4118 Holiday's Accommodation 树上统计
- hdu 4118 Holiday's Accommodation 树形dp
- hdu 4118 Holiday's Accommodation 树形dp
- hdu Holiday's Accommodation
- Regional_2011_H Holiday's Accommodation
- Holiday's Accommodation
- Holiday's Accommodation HDU
- hdu Holiday's Accommodation dfs
- hdu 4118 Holiday's Accommodation 2011 Asia ChengDu Regional Contest
- HDU 4118--Holiday's Accommodation(树形dp)
- [HDU 4118]Holiday's Accommodation[图论][非递归dfs]
- HDU 4118 Holiday's Accommodation(树形DP)
- 割据的逻辑:中国法律服务市场的生态分析
- zoj3761(BFS)
- BITACM 1047 Fibonacci Numbers
- 查找循环数组中元素位置(无重复元素)
- poj 1979
- HDOJ 4118 Holiday's Accommodation
- java.io.NotSerializableException的解决办法
- Android中对Log日志文件的分析
- 题目1204:农夫、羊、菜和狼的故事
- poj2503 Barbelfish
- linux格林时间 date +%s
- SQLServer 排名函数摘录
- (转)Visual Studio 2010常见问题总结
- POJ 3009