Description

在山上一共有N个站点需要检查,检查员从山顶出发去各个站点进行检查,各个站点间有且仅有一条通路,检查员下山前往站点时比较轻松,而上山时却需要额外的时间,问最后检查员检查完所有站点时所需要的额外时间最少是多少。

Input

包含多组数据 每组数据输入第一行为一个整数N 表示站点个数(1<=N<=100000),接下去N-1 行 每行3个整数 x,y,z(1<=z<=10000) 检查站x为检查站y的父节点,x,y之间有一条通路,从y到x需要额外z的时间。(父节点在子节点上方,山顶固定标号为1)

Output

输出一行一个整数表示最少需要花费的额外时间。

Sample Input

6

1 2 1

2 4 1

1 3 1

3 5 1

3 6 1

Sample Output

3

题目乍一看像旅行商问题,但是n这么大,显然不是这么玩的。

考虑到树形结构的特殊性,就是从左子树到右子树,一定要进过父节点才能到达,这点是跟图不一样的。

于是从过程来看,我如果设p[i]表示遍历以i为根的子树所需的最小花费。all[i]表示遍历完并回到i结点的最小花费。当然,起始都是从i结点出发。

那么对于i来说,all[i] = sum(all[k]+dis[k][i])(k是i的子节点)

p[i] = min(sum(all[k]+dis[k][i])+p[j])(其中k != j)

这样的话,用记忆化搜索,便可以得到所有的p和all。p[1]即为所要求。

复杂度是O(n)

但是直接从结果上来看的话,整个遍历的路径,一定是每个线段都一上一下各一次,但是有一条从根部到底部的路径只下不上。(这个从之前的特殊性前提也可以推来)

这样便只需要找这样一条耗费最大的路径,然后用所有路径的和减去它即可。

复杂度也是O(n),但是可以省掉两个数组。

代码:(dp)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; const int maxN = 1e5+;
int n;
LL all[maxN], p[maxN]; inline LL myMin(LL x, LL y)
{
if (x == -)
return y;
else
return x < y ? x : y;
} //链式前向星
struct Edge
{
int to, next;
int val;
}edge[maxN]; int head[maxN], cnt; void addEdge(int u, int v, int val)
{
edge[cnt].to = v;
edge[cnt].val = val;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} void input()
{
initEdge();
memset(all, -, sizeof(all));
memset(p, -, sizeof(p));
int x, y, z;
for (int i = ; i < n; ++i)
{
scanf("%d%d%d", &x, &y, &z);
addEdge(x, y, z);
}
} void dfs(int now)
{
LL s = ;
int to;
for (int k = head[now]; k != -; k = edge[k].next)
{
to = edge[k].to;
if (all[to] == -)
dfs(to);
s += all[to]+edge[k].val;
}
all[now] = s;
for (int k = head[now]; k != -; k = edge[k].next)
{
to = edge[k].to;
p[now] = myMin(p[now], s-all[to]-edge[k].val+p[to]);
}
if (p[now] == -)
p[now] = ;
} void work()
{
dfs();
printf("%I64d\n", p[]);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
input();
work();
}
return ;
}

ACM学习历程—FZU2195 检查站点(树形DP || 贪心)的更多相关文章

  1. ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

    Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...

  2. ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)

    Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...

  3. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  4. ACM学习历程—HDU4415 Assassin’s Creed(贪心)

    Problem Description Ezio Auditore is a great master as an assassin. Now he has prowled in the enemie ...

  5. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  6. ACM学习历程——POJ 1700 Crossing River(贪心)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...

  7. ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

    Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...

  8. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  9. [BZOJ1596] [Usaco2008 Jan]电话网络(树形DP || 贪心)

    传送门 1.树形DP #include <cstdio> #include <cstring> #include <iostream> #define N 1000 ...

随机推荐

  1. POJ 1694 An Old Stone Game【递归+排序】

    链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  2. 洛谷 P1558 色板游戏

    洛谷 题解里面好像都是压位什么的, 身为蒟蒻的我真的不会, 所以就来谈谈我的30颗线段树蠢方法吧! 这题初看没有头绪. 然后发现颜色范围好像只有30: 所以,我就想到一种\(sao\)操作,搞30颗线 ...

  3. Ext部署在本地tomcat下运行例子

    我本地用的ext6+,从官网下载好Ext后解压到D盘,然后打开tomcat的server.xml,在Host标签内配置 <Context path="/ext-6.2.0" ...

  4. ubuntu 安装vagrant过程

    Ubuntu安装vagrant时需要首先安装virtualBox. Step1: 在https://www.virtualbox.org/wiki/Linux_Downloads 下载ubuntu对应 ...

  5. ABAP动态生成经典应用之Dynamic SQL Excute 程序

    [转自http://blog.csdn.net/mysingle/article/details/678598]开发说明:在SAP的系统维护过程中,有时我们需要修改一些Table中的数据,可是很多Ta ...

  6. 每天一个Linux命令(8)cat命令

    cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的type命令. 注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容.因此,一般用more等命 ...

  7. c的详细学习(4)选择结构与循环结构的编程练习

        本节介绍c语言的编程实例.     (1)求方程ax^2+bx+0的解: 方程有解有以下几种可能: 1.b^2-4ac=0,方程有两个相等实根. 2.b^2-4ac>0,方程有两个不等实 ...

  8. 第二十三篇、IO多路复用 二

    基于IO多路复用实现socket的连接 下面流程:1.先创建列表,让自己的socket对象传入,然后遍历select第一个参数2.客户端连接,那么客户端socket就传入到了列表里面,3.遍历,接收客 ...

  9. centos 中 增强web服务器安全

    一.修改ssh连接的默认端口: 1.1 用root 连接进入系统: 1.2 修改ssh的配置文件 #vi /etc/ssh/sshd_config 在13行找到#Port 22 (默认端口22) 1. ...

  10. 复选框 ajax取得后台页面

    首先判断是否勾选了复选框 $("input[name='product']").click(function () { if($(this).prop("checked& ...