树形dp。

先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解。

然后从上到下可以推出所有答案。细节较多,很容易写错。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
#include<ctime>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const int maxn=;
vector<int>G[maxn],t[][maxn][];
int T,n,v[maxn],dp[maxn][],ans[maxn],sz;
struct Edge{int u,v,w;}e[*maxn];
bool flag[maxn]; void add(int a,int b,int c)
{
e[sz].u=a; e[sz].v=b; e[sz].w=c;
G[a].push_back(sz++);
} void dfs(int x,int f)
{
flag[x]=; dp[x][]=dp[x][]=v[x]; int i; if(f==) i=; else i=G[x].size()-;
while()
{
if(f==&&i==G[x].size()) break; if(f==&&i==-) break; int id=G[x][i],to=e[id].v;
if(flag[to])
{
t[f][x][].push_back(dp[x][]); t[f][x][].push_back(dp[x][]);
if(f==) i++; else i--; continue;
} dfs(to,f); int t1=dp[x][], t2=dp[x][]+dp[to][]-e[id].w, t3=dp[x][]+dp[to][]-*e[id].w;
dp[x][]=max(t1,max(t2,t3));
dp[x][]=dp[x][]+max(dp[to][]-*e[id].w,); t[f][x][].push_back(dp[x][]); t[f][x][].push_back(dp[x][]);
if(f==) i++; else i--;
} } void get(int x,int a,int b,int c)
{
flag[x]=; int t1=dp[x][];
int t2=dp[x][]+b-c;
int t3=a+dp[x][]-*c; ans[x]=max(t1,max(t2,t3)); int SZ=G[x].size(); for(int i=;i<SZ;i++)
{
int id=G[x][i],to=e[id].v;
if(flag[to]) continue;
int na=,nb=,dp0=,dp1=, x1=,x2=,y1=,y2=;
if(i->=) x1=t[][x][][i-];
if(SZ--(i+)>=) x2=t[][x][][SZ--(i+)];
if(i->=) y1=t[][x][][i-];
if(SZ--(i+)>=) y2=t[][x][][SZ--(i+)];
dp0=x1+x2; if(x1!=&&x2!=) dp0=dp0-v[x];
int k1,k2;
k1=x1+y2; if(x1!=&&y2!=) k1=k1-v[x];
k2=x2+y1; if(x2!=&&y1!=) k2=k2-v[x];
dp1=max(k1,k2);
dp0=max(dp0,v[x]); dp1=max(dp1,v[x]);
na=max(dp0,dp0-*c+a); nb=max(dp1,max(dp0+b-c,a+dp1-*c));
get(to,na,nb,e[id].w); } } int main()
{
scanf("%d",&T); int cas=;
while(T--)
{
scanf("%d",&n); sz=; memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
{
G[i].clear();
t[][i][].clear(); t[][i][].clear();
t[][i][].clear(); t[][i][].clear();
} for(int i=;i<=n;i++) scanf("%d",&v[i]);
for(int i=;i<=n-;i++)
{
int u,v,w; scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
} memset(flag,,sizeof flag); dfs(,);
memset(flag,,sizeof flag); dfs(,); memset(flag,,sizeof flag); get(,,,); printf("Case #%d:\n",cas++);
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
}
return ;
}

HDU 5834 Magic boy Bi Luo with his excited tree的更多相关文章

  1. 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 ...

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

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

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

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

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

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+

  5. 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 ...

  6. 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: ...

  7. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...

  8. 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 ...

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

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

随机推荐

  1. ASP.NET 程序发布详细过程

    前言 ASP.NET网站的发布,无论是初学者还是高手,在程序的发布过程中或多或少会存在一些问题,譬如VS发布ASP.NET程序失败.IIS安装失败.IIS发布失败.局域网内不能访问 配置文件错误.权限 ...

  2. Jquery中toggle的用法详情

    jquery ----toggle([speed],[easing],[fn]) 用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的click事件 如果元素是可见的,切换为隐藏的:如果元素是隐藏 ...

  3. vmare虚拟化解决方案

    最近刚入职一家公司!主要是做vmare虚拟化的,虽然跟openstack不太沾边,但现在很多openstack都会兼容vmare程序,最终会如何我拭目以待,今天写这个也是做一个总结. 虚拟化.云计算. ...

  4. 使用redis缓存数据需要注意的问题以及个人的一些思考和理解

    之前我有博客也尝试过使用redis,在实际的项目中确实作用挺大的.至少对于数据的频繁读取来说都起着至关重要的作用. 但是随着技术的学习,慢慢的业务要复杂起来,以后也许会用到redis集群,所以在这边查 ...

  5. Docker集群实验环境布署--swarm【4 管理组件--manager】

    主机分配如下,支持双活,中断其中1台,primary会通过consul自动重新选举   10.40.100.141 docker-manager0.venic.com 10.40.100.142 do ...

  6. 国内下载比较快的Maven仓库镜像

    #收集的比较快的maven仓库 http://maven.wso2.org/nexus/content/groups/public/ http://jcenter.bintray.com/http:/ ...

  7. Vmware(vmdk)虚拟机到hyperv(vhd)虚拟机转换

    1.关闭Vmware所有虚拟机 2.合并Vmware磁盘 通常磁盘都分开储存 targetDisk.vmdk 3.转换vmdk到vhd 安装winimage http://www.winimage.c ...

  8. redis面试总结

    http://blog.csdn.net/guchuanyun111/article/category/6335900 (1)什么是redis? Redis 是一个基于内存的高性能key-value数 ...

  9. Java学习笔记-File

    //文件操作 //2015年4月30日15:40:21 package com.alfredsun.first.main; import java.io.File; import java.io.IO ...

  10. wpf 寻找某个控件下的子控件

    /// <summary> /// 寻找某个控件下的子控件 /// </summary> /// <typeparam name="ChildType" ...