HDU - 4118 Holiday's Accommodation
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.
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.
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
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's Accommodation的更多相关文章
- HDU 4118 Holiday's Accommodation
Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Jav ...
- HDU 4118 Holiday's Accommodation(树形DP)
Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 K (Jav ...
- HDU 4118 Holiday's Accommodation (dfs)
题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值. 析:对于每边条,我们可以这么考虑,它的左右两边的点数最少的就是要加的数目,因为最好的情况就是左边到右 ...
- HDU 4118 树形DP Holiday's Accommodation
题目链接: HDU 4118 Holiday's Accommodation 分析: 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍. 代码: #include<iostrea ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- HDU 3966 Aragorn's Story(树链剖分)
HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...
- hdu 5282 Senior's String 两次dp
题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 3177 Crixalis's Equipment(贪婪)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...
- HDU - 5186 - zhx's submissions (精密塔尔苏斯)
zhx's submissions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
随机推荐
- 1.String、StringBuffer与StringBuilder之间区别
1.三者在执行速度方面的比较:StringBuilder > StringBuffer > String 2.String <(StringBuffer,StringBuild ...
- CentOS7查看开放端口命令及开放端口号
CentOS 7查看以开放端口命令:firewall-cmd —list-ports 查看端口是否开放命令:第一个方法就是使用lsof -i:端口号命令行,例如lsof -i:80.如果没有任何信息输 ...
- css 单行/多行文字垂直居中问题
例子可以直接看这里http://www.zhangxinxu.com/study/200911/line-height-text-v-center.html 这篇文章中有一点点解释http://blo ...
- 结束autocad异常进程
近日在做CAD自动化数据处理,程序在服务器上运行,运行时间长了会发生异常“autocad application 已停止工作”,这个时候需要通过守护程序去重启CAD, 通过CMD命令“@taskkil ...
- Linux CentOS 6.5 + Apache + Mariadb + PHP环境搭建
Web自动化测试-服务端测试环境部署 by:授客 目录 一. 二. 三. 四. 五. 六. 七. 八. 九. 十. 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.c ...
- Python图像识别(聚类)
# -*- coding: utf-8 -*- """ Created on Fri Sep 21 15:37:26 2018 @author: zhen "& ...
- python if 判断
#!/usr/bin/env python# -*- conding:utf-8 -*- if 条件: 执行1 执行2else: 执行3 if else 后面需要加: python严格缩进,内容1和内 ...
- jquery中ajax的dataType的各种属性含义
参考ajax api文档:http://www.w3school.com.cn/jquery/ajax_ajax.asp dateType后接受的参数参数类型:string 预期服务器返回的数据类型. ...
- http的断点续传
要实现断点续传的功能,通常都需要客户端记录下当前的下载进度,并在需要续传的时候通知服务端本次需要下载的内容片段. HTTP1.1协议(RFC2616)中定义了断点续传相关的HTTP头 Range和Co ...
- orcl regexp_like 的用法
oracle10g以上支持正则表达式的函数主要有下面四个:1.REGEXP_LIKE :与LIKE的功能相似2.REGEXP_INSTR :与INSTR的功能相似3.REGEXP_SUBSTR :与S ...