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)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <string>
  10. #include <algorithm>
  11. #define LL long long
  12.  
  13. using namespace std;
  14.  
  15. const int maxN = 1e5+;
  16. int n;
  17. LL all[maxN], p[maxN];
  18.  
  19. inline LL myMin(LL x, LL y)
  20. {
  21. if (x == -)
  22. return y;
  23. else
  24. return x < y ? x : y;
  25. }
  26.  
  27. //链式前向星
  28. struct Edge
  29. {
  30. int to, next;
  31. int val;
  32. }edge[maxN];
  33.  
  34. int head[maxN], cnt;
  35.  
  36. void addEdge(int u, int v, int val)
  37. {
  38. edge[cnt].to = v;
  39. edge[cnt].val = val;
  40. edge[cnt].next = head[u];
  41. head[u] = cnt;
  42. cnt++;
  43. }
  44.  
  45. void initEdge()
  46. {
  47. memset(head, -, sizeof(head));
  48. cnt = ;
  49. }
  50.  
  51. void input()
  52. {
  53. initEdge();
  54. memset(all, -, sizeof(all));
  55. memset(p, -, sizeof(p));
  56. int x, y, z;
  57. for (int i = ; i < n; ++i)
  58. {
  59. scanf("%d%d%d", &x, &y, &z);
  60. addEdge(x, y, z);
  61. }
  62. }
  63.  
  64. void dfs(int now)
  65. {
  66. LL s = ;
  67. int to;
  68. for (int k = head[now]; k != -; k = edge[k].next)
  69. {
  70. to = edge[k].to;
  71. if (all[to] == -)
  72. dfs(to);
  73. s += all[to]+edge[k].val;
  74. }
  75. all[now] = s;
  76. for (int k = head[now]; k != -; k = edge[k].next)
  77. {
  78. to = edge[k].to;
  79. p[now] = myMin(p[now], s-all[to]-edge[k].val+p[to]);
  80. }
  81. if (p[now] == -)
  82. p[now] = ;
  83. }
  84.  
  85. void work()
  86. {
  87. dfs();
  88. printf("%I64d\n", p[]);
  89. }
  90.  
  91. int main()
  92. {
  93. //freopen("test.in", "r", stdin);
  94. while (scanf("%d", &n) != EOF)
  95. {
  96. input();
  97. work();
  98. }
  99. return ;
  100. }

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. Solr6.5与mysql集成建立索引

    首先在solrconfig.xml(我的是保存在/usr/local/tomcat/solrhome/mycore/conf/下)的<requestHandler name="/sel ...

  2. 在UIWebView中设置cookie

     本文转载至 http://blog.csdn.net/chengyakun11/article/details/8863878 项目中,需要在打开3g网页时,通过cookie传递一些信息. 实现代码 ...

  3. TP框架的修改,删除

    先把数据库的素具显示出来 public function xiugai() { $code= "n001";//修改的主键值 $n = M("nation"); ...

  4. poj3708(公式化简+大数进制装换+线性同余方程组)

    刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...

  5. 【BZOJ3926】[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机

    [BZOJ3926][Zjoi2015]诸神眷顾的幻想乡 Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝 ...

  6. Centos设置开机启动Apache和Mysql

    先用chkconfig --list查询apache和mysql服务是否存在,不存在则需要手动添加 [root@centos64 vsftpd]# chkconfig --list 测试存在,只需要开 ...

  7. twig 截取字符串

    <p>{{content|slice(0,100)}}</p> slice()截取content变量值,从0到100

  8. Docker学习总结之docker创建私有仓库(private Repositories)

    Docker 创建 Private Repositories 前言 基于GFW的缘故,国内大陆基本无法pull国外的镜像,更别说官方的index了.如果images无法pull下来,那么docker就 ...

  9. 深度问答之提取语料,导入了yml模块

    根据目录下的yml文件循环创建同名文件夹,并从yml文件读取问答并给每个文件夹写入question和answer文件 #!/usr/bin/env python # coding:utf8 # aut ...

  10. 【LeetCode】 数相加组合 Combination Sum

    描述 Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), ...