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. 从零开始学习html(十二)CSS布局模型——上

    一.css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了. 布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之 ...

  2. 浅谈 Event loop (事件循环)

    从Event Loop谈JS的运行机制 先来理解一个概念: JS分为同步任务和异步任务 同步任务都在主线程上执行,形成一个执行栈 Execute Content Stack 主线程之外,事件触发线程管 ...

  3. 【java基础】基础小总结

    学习java,将自己的心得或总结写下来吧. Java 标识符 标识符由字母,下划线(_),美元符($)和数字组成. 标识符不能以数字开头. 标识符不能使java关键字. 标识符对大小写敏感. Java ...

  4. Echarts之悬浮框中的数据排序

    Echarts非常强大,配置也非常的多,有很多细节需要深入研究.详解一下关于悬浮框中的数据排序问题 悬浮框的数据排序默认是根据series中的数据位置排序的,在我们想自定义排序时,在echarts的配 ...

  5. AngularJS学习之 ui router

    1.安装 bower install --save angular_ui-router 2.在项目主页面 index.html中添加 <div ui-view="">& ...

  6. [基础知识]row类visible使用

    使用row的visibe属性,要反向遍历rowset,因为如果正向遍历,rowset是实时变化的,行号是错误的.正确代码如下: Local integer &k; For &k = & ...

  7. 企业如何选择合适的BI工具?

    在没认清现状前,企业当然不能一言不合就上BI. BI不同于一般的企业管理软件,不能简单归类为类似用于提高管理的ERP和WMS,或用于提高企业效率的OA.BPM.BI的本质应该是通过展现数据,用于加强企 ...

  8. Jaguar_websocket结合Flutter搭建简单聊天室

    1.定义消息 在开始建立webSocket之前,我们需要定义消息,如:发送人,发送时间,发送人id等.. import 'dart:convert'; class ChatMessageData { ...

  9. JSTL核心标签库——错误处理标签

    <c:catch>标签 Demo: 错误发生时,不转发至其他网页来显示错误信息,而在当前网页捕捉异常,并显示相关信息. <%@page contentType="text/ ...

  10. LeetCode题解之Pascal's Triangle II

    1.题目描述 2.题目分析 题目要求返回杨辉三角的某一行,需要将杨辉三角的某行的全部计算出来. 3.代码实现 vector<int> getRow(int rowIndex) { ) ,) ...