1. // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
  2. // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以走多次,问从每个点出发的最大获益
  3. // 思路:
  4. // dp[i]: 从i点出发回到i点的最大值
  5. // d[i][0] 从i点出发不回来的最大值
  6. // d[i][1] 从i点出发取最大值的下一个点
  7. // d[i][2] 从i点出发取次大值
  8. // dfs1处理出这四个
  9. // 然后,从1开始转移,分别DP求子节点从父亲转移过来的最大值,和从父亲出去不回来的最大值
  10.  
  11. #include <bits/stdc++.h>
  12. using namespace std;
  13. #define LL long long
  14. const double inf = 123456789012345.0;
  15. const LL MOD =100000000LL;
  16. const int N =1e5+;;
  17. #define clc(a,b) memset(a,b,sizeof(a))
  18. const double eps = 1e-;
  19. void fre() {freopen("in.txt","r",stdin);}
  20. void freout() {freopen("out.txt","w",stdout);}
  21. inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;}
  22.  
  23. vector<pair<int,int> > g[N];
  24. int a[N],ans[N];
  25. int dp[N],d[N][];
  26. void dfs1(int u,int f){
  27. dp[u]=a[u];
  28. for(int i=;i<(int)g[u].size();i++){
  29. int v=g[u][i].first;
  30. int val=g[u][i].second;
  31. if(v==f) continue;
  32. dfs1(v,u);
  33. dp[u]+=max(,dp[v]-*val);
  34. }
  35. d[u][]=d[u][]=a[u];
  36. d[u][]=-;
  37. for(int i=;i<(int)g[u].size();i++){
  38. int v=g[u][i].first;
  39. int val=g[u][i].second;
  40. if(v==f) continue;
  41. int tem=dp[u]-max(,dp[v]-*val)+max(,d[v][]-val);//当前选择路径不回来的最大值
  42. if(tem>d[u][]){
  43. d[u][]=d[u][];
  44. d[u][]=tem;
  45. d[u][]=i;
  46. }
  47. else if(tem>d[u][]){
  48. d[u][]=tem;
  49. }
  50. }
  51. }
  52.  
  53. //no从父亲不回来的最大值,yes从父亲回来的最大值
  54. void dfs2(int u,int f,int no,int yes){
  55. ans[u]=max(dp[u]+no,yes+d[u][]);
  56. for(int i=;i<(int)g[u].size();i++){
  57. int v=g[u][i].first;
  58. int val=g[u][i].second;
  59. if(v==f) continue;
  60. int tem1,tem2;
  61. if(i==d[u][]) tem1=max(,d[u][]-max(,dp[v]-*val));//从父亲出去但不是最优方案的最大值
  62. else tem1=max(,d[u][]-max(,dp[v]-*val));
  63. tem2=max(,dp[u]-max(,dp[v]-*val));//从父亲出去又回来的最大值
  64. tem1=max(,max(tem1+yes-val,tem2+no-val));//儿子节点从父亲转移过来的是 max(父亲儿子节点出去不回来+父亲的父亲出去回来的最大值,父亲儿子节点出去回来+父亲的父亲出去不回来的最大值)
  65. tem2=max(,yes+tem2-*val);
  66. dfs2(v,u,tem1,tem2);
  67. }
  68. }
  69.  
  70. int main(){
  71. int T;
  72. scanf("%d",&T);
  73. for(int cas=;cas<=T;cas++){
  74. int n;
  75. scanf("%d",&n);
  76. for(int i=;i<=n;i++) g[i].clear();
  77. for(int i=;i<=n;i++) scanf("%d",&a[i]);
  78. for(int i=;i<n;i++){
  79. int u,v,x;
  80. scanf("%d%d%d",&u,&v,&x);
  81. g[u].push_back(make_pair(v,x));
  82. g[v].push_back(make_pair(u,x));
  83. }
  84. dfs1(,-);
  85. dfs2(,-,,);
  86. printf("Case #%d:\n",cas);
  87. for(int i=;i<=n;i++) printf("%d\n",ans[i]);
  88. }
  89. return ;
  90. }

树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree的更多相关文章

  1. 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+

  2. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  3. 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

    Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...

  4. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

  5. hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】

    题目链接 hdu5834 题解 思路很粗犷,实现很难受 设\(f[i][0|1]\)表示向子树走回来或不回来的最大收益 设\(g[i][0|1]\)表示向父亲走走回来或不回来的最大收益 再设\(h[i ...

  6. HDU5834 Magic boy Bi Luo with his excited tree (树形DP)

    题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...

  7. 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...

  8. hdu 5834 Magic boy Bi Luo with his excited tree 树形dp+转移

    Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 13107 ...

  9. HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...

随机推荐

  1. 应用程序加载外部字体文件(使用AddFontResource API函数指定字体)

    /* MSDN: Any application that adds or removes fonts from the system font table should notify other w ...

  2. shell编程基础(5)---循环指令

    while类型的循环 while类型的循环是不定循环的一种,每一次循环都会验证给出的循环条件,判断是否要进行下一次循环.linux中while循环的写法和c语言中很想,但是条件给出的方式有些区别. 首 ...

  3. IO多路复用的几种实现机制的分析

    http://blog.csdn.net/zhang_shuai_2011/article/details/7675797 select,poll,epoll都是IO多路复用的机制.所谓I/O多路复用 ...

  4. 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

    题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...

  5. ViewPager介绍和使用说明

    1   ViewPager实现的功能 和实际运行的效果图示意 ViewPager类提供了多界面切换的新效果.新效果有如下特征: [1] 当前显示一组界面中的其中一个界面. [2] 当用户通过左右滑动界 ...

  6. 【iOS-cocos2d-X 游戏开发之九】Cocos2dx利用CCSAXParser解析xml数据&CCMutableDictionary使用与注意!

    本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/694.html ☞ ...

  7. 修改placeholder属性

    input::-webkit-input-placeholder{ font-size:12px;}input:-ms-input-placeholder{ font-size:12px;}input ...

  8. matlab中,计算,记录,程序运行,起始,结束 时间,间隔 &matlab中 tic,toc函数的用法

    Tic和toc函数可以计算运行一段时间的代码. 例如: clc tic d=zeros(1,10000); for i=1:10000 d(i)=i; end toc tic c=1; for i=1 ...

  9. 快速搭建建SSH服务

    一般来说如果用Ubuntu作为服务器,我们经常需要通过其他客户端远程连接它. 远程连接需要使用SSH,这里列出了一个快速完成这一任务的方法. 键入命令 # sudo apt-get install o ...

  10. Solr与Mysql简单集成

    Solr与Mysql数据库的集成,实现全量索引.增量索引的创建. 基本原理很简单:在Solr项目中注册solr的DataImportHandler并配置Mysql数据源以及数据查询sql语句.当我们通 ...