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. iTerm通过堡垒机自动登录服务器

    为了保障网络和数据安全,越来越多公司使用堡垒机.iTerm作为一个好用的终端利器,要实现自动通过堡垒机登录服务器的方式有多种.下面我就来介绍一种通过expect脚本的方式完成配置. 第一步,进入/us ...

  2. AutoCAD.net支持后台线程-Socket通讯

    最近因为公司项目的需求,CAD作为服务端在服务器中常驻运行,等待客户端远程发送执行任务的指令,最终确认用Socket-tcp通讯,CAD需要实时监听客户端发送的消息,这时就需要开启线程执行Socket ...

  3. ActiveReports 区域报表中的事件介绍

    1.仅触发一次的事件 以下是在报表的处理过程中仅触发一次的所有事件这些事件在报表的处理周期中仅在最开始和结束前触发一次. ReportStart 该事件在DataInitialize事件触发之前发生. ...

  4. python学习笔记——python JSON

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.JSON 函数 使用 JSON 函数需要导入 json 库:import json ...

  5. 排错-windows平台下访问oracle em出现空白的解决方法

    排错-windows平台下访问oracle em出现空白的解决方法 by:授客 QQ:1033553122 问题描述 IE浏览器本地访问oem,出现空白页面,就左上角有一行字符 http://loca ...

  6. WOSA/XFS PTR Form解析库—头文件

    class AFX_EX_CLASS CNuXfsForm {public: CNuXfsForm(); ~CNuXfsForm(); /******************************* ...

  7. Apache,PHP,MySQL独立安装

    最近在工作中常常接触到PHP,自己也写过一些简单的PHP页面.我们知道PHP是在服务器端运行的脚本语言,因此我们需要配置服务器环境.之前为了省事直接使用的是wamp集成环境,但是突然某一天领导要求我们 ...

  8. Linux网络配置和网络诊断命令介绍

    方法/步骤 1 在接下来的讲解中,讲解的Linux网络配置和网络诊断的命令有: ifconfig.ping.netstat.traceroute.dig和nslookup.host.hostname. ...

  9. Prometheus Node_exporter 之 Network Traffic Detail

    Network Traffic Detail /proc/net/dev 1. Network Traffic by Packets type: GraphUnit: packets/secLabel ...

  10. windows10如何打开vhd文件

    本人电脑安装了Visual Studio 2017,但是由于项目需求需要Core SDK(2.0)的版本支持,也就是2017最新版.所以现在需要利用visual Studio 2017最新版本的安装包 ...