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.
 
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.
 
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

题意:一颗树。相应1-n的结点,如今要求是每一个结点原本的人都走到不一样的结点去,每一个人都有路程,求总的最大路程

思路:对于每条边我们能想到的是:这条边左边的结点都跑到右边去,右边的 结点都跑到左边去。所以每条边。都会被走min{left[num], sum-left[nu,]}*2次,依据这个原则我们进行树形DP。可是这题递归的写法会跪。所以仅仅能手动DFS

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 200010; struct Node {
int to, next;
int len;
} edge[maxn<<1];
int head[maxn], num[maxn], cnt, n;
int sta[maxn], vis[maxn];
ll ans; void init() {
cnt = 0;
memset(head, -1, sizeof(head));
memset(num, 0, sizeof(num));
} void add(int u, int v, int len) {
edge[cnt].to = v;
edge[cnt].len = len;
edge[cnt].next = head[u];
head[u] = cnt++; edge[cnt].to = u;
edge[cnt].len = len;
edge[cnt].next = head[v];
head[v] = cnt++;
} void dfs(int u) {
memset(vis, 0, sizeof(vis));
int top = 0;
sta[top++] = u;
vis[u] = 1;
while (top > 0) {
int flag = 1;
int cur = sta[top-1];
for (int i = head[cur]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (vis[v]) continue;
flag = 0;
sta[top++] = v;
vis[v] = 1;
} if (flag == 0) continue;
top--; for (int i = head[cur]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (num[v] != 0) {
num[cur] += num[v];
ans += (ll) edge[i].len * min(num[v], n - num[v]);
}
}
num[cur]++;
}
} int main() {
int t, cas = 1;
int u, v, w;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
init();
ans = 0;
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
dfs(1);
printf("Case #%d: %I64d\n", cas++, ans*2);
}
return 0;
}

HDU - 4118 Holiday&#39;s Accommodation的更多相关文章

  1. HDU 4118 Holiday's Accommodation

    Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Jav ...

  2. HDU 4118 Holiday's Accommodation(树形DP)

    Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Jav ...

  3. HDU 4118 Holiday's Accommodation (dfs)

    题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值. 析:对于每边条,我们可以这么考虑,它的左右两边的点数最少的就是要加的数目,因为最好的情况就是左边到右 ...

  4. HDU 4118 树形DP Holiday's Accommodation

    题目链接:  HDU 4118 Holiday's Accommodation 分析: 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍. 代码: #include<iostrea ...

  5. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  6. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

  7. hdu 5282 Senior&#39;s String 两次dp

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...

  8. HDU 3177 Crixalis&#39;s Equipment(贪婪)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...

  9. HDU - 5186 - zhx&#39;s submissions (精密塔尔苏斯)

    zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. HttpClient的用法

    客户端模拟http请求工具 Postmen(谷歌插件).RestClient 服务器模拟http请求工具 httpclient.HttpURLConnection httpCient请求代码 /** ...

  2. npm install、npm init、npm update、npm uninstall和package.json

    npm install 安装本地包 npm install <package_name>:这个命令将在当前目录中创建node_modules目录(如果尚不存在),并将该软件包下载到该目录. ...

  3. PeopleSoft单点登录工作原理

    单点登录是指用户在仅通过一次身份验证后,可以在多个应用程序中自由切换. 这意味着用户只需要一次登录信息,只要他点击指向其他应用程序的链接,安全信息就会自动从一个应用程序传递到另一个应用程序,用户就不会 ...

  4. WampServer下修改和重置MySQL密码

    Wampserver PHP环境中mysql数据库登录密码的修改和重置,mysql命令.     工具/原料   电脑Windows系统 WampServer 方法/步骤1     启动WampSer ...

  5. iis7 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理

    cmd 输入 C:\Windows\Microsoft.NET\Framework\V4.0.30319\aspnet_regiis -i

  6. IIS8发布Asp.net MVC程序后出现404错误,处理程序staticFile

    新部署的虚拟机,运行Asp.net MVC程序,出现如下图错误: 解决方法: 添加功能和角色->添加角色->Web服务器IIS->应用程序开发->Asp.net3.5 /Asp ...

  7. Linux Ubuntu16.04LTS安装TensorFlow(CPU-only,python3.7)——使用Anaconda安装

    1.安装Anaconda(在此不再赘述) 2.用Conda安装TensorFlow 1)建立TensorFlow运行环境并激活 conda create -n tensorflow pip pytho ...

  8. shell變量和數組

    我們要知道shell是一個很重要的腳本能幫助我們完成很多事情 shell語言其實和很多的語言的語法是差不多的 變量: 變量的定義很簡單的,但是等號兩邊是不可以有空格的(不能有空格) 命名只能使用英文字 ...

  9. fab提供远程IP和账号密码

    #!/usr/bin/python #-*- coding: UTF-8 -*- from fabric.api import * from fabric.context_managers impor ...

  10. cisco查看机框 板卡 电源 SN 风扇环境运行状态和一些常用命令 巡检命令

    查看设备运行环境及状态 show environment 查看设备环境show environment temperature --查设备温度 show environment fans --查看设备 ...