树形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. MVC源码解析 - 进入CLR

    这一篇是转载自汤姆大叔的一篇随笔. IIS 5 的 ASP.net 请求处理过程 IIS5核心特征是:IIS是允许在一个叫InetInfo.exe的进程上的,所以无论是aspx页面还是html页面都是 ...

  2. iOS之上架打包时报错:ERROR ITMS-90086: "Missing 64-bit support.

    根据错误信息在网上基本找到的解决方法是设置targets中build settings中的architectures中的内容 这是因为现在提交的app必须支持64位.  

  3. C++继承、多态、虚函数

    #答案:http://www.cnblogs.com/tinaluo/articles/6290994.html 1 编写程序:定义一个抽象基类sharpe,只有包含一个求面积的纯虚函数,派生出矩形类 ...

  4. Tweak 中系统方法写入文件到根目录下面失败

    Tweak 中系统方法写入文件到根目录下面失败 失败原因: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t ...

  5. IOS 成员变量,全局变量,局部变量定义,static与extern的区别

    IOS 成员变量,全局变量,局部变量定义,static与extern的区别 1,先说定义 1)成员变量定义:生存与该类的生命周期,变量存活周期跟你定义的该类实体对象一样:作用域是整个实体对象:可以在h ...

  6. centos7 install rvm

    不管其他,先按要求更新一下包 yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel li ...

  7. MySQL连接无法解析HOST主机名

    #1042 - Can't get hostname for your address 使用IP链接或域名链接都可能遇到这个问题 解决办法: my.ini 或 my.cnf 末尾添加 skip-nam ...

  8. Python学习笔记——基础篇1【第三周】——set集合

    set集合 不允许重复的元素出现(相当于特殊的列表) set 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 练习:寻找差异 # 数据库中原有 old_dic ...

  9. Java 并发 线程的生命周期

    Java 并发 线程的生命周期 @author ixenos 线程的生命周期 线程状态: a)     New 新建 b)     Runnable 可运行 c)     Running 运行 (调用 ...

  10. [河南省ACM省赛-第四届] 序号互换 (nyoj 303)

    相似与27进制的转换 #include<iostream> #include<cstdio> #include<cstring> #include<strin ...