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: 131072/131072 K (Java/Others)
Total Submission(s): 1037 Accepted Submission(s): 298
You may attention that every V[i] can be taken only once, but for some C[i] , you may cost severial times.
Now, Bi Luo define ans[i] as the most value can Bi Luo gets if Bi Luo starts at node i.
Bi Luo is also an excited boy, now he wants to know every ans[i], can you help him?
Four each test:
The first line contain an integer N(N≤105).
The next line contains N integers V[i], which means the treasure’s value of node i(1≤V[i]≤104).
For the next N−1 lines, each contains three integers u,v,c , which means node u and node v are connected by an edge, it's cost is c(1≤c≤104).
You can assume that the sum of N will not exceed 106.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const int mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=(1<<20)+10;
const int maxn=1e5+110;
const double eps=1e-12; int n,cnt,head[maxn],a[maxn];
int down[maxn][2],up[maxn][2],max1[maxn],max2[maxn],temp[maxn],cost[maxn];
struct Edge
{
int from,to,next,val;
}edge[2*maxn];
inline void add_edge(int s,int e,int va)
{
edge[cnt].from=s;
edge[cnt].to=e;
edge[cnt].next=head[s];
edge[cnt].val=va;
head[s]=cnt++;
}
inline void Init()
{
cnt=0;
for(int i=0;i<=n;i++)head[i]=-1;
}
void dfs(int cur,int fa,int va)
{
down[cur][1]=a[cur];
cost[cur]=va;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int x=edge[i].to;
if(x==fa)continue;
dfs(x,cur,edge[i].val);
if(down[x][1]-2*edge[i].val>=0)down[cur][1]+=down[x][1]-2*edge[i].val;
}
}
void dfs1(int cur,int fa)
{
down[cur][0]=a[cur];
temp[cur]=max1[cur]=max2[cur]=0;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int x=edge[i].to;
if(x==fa)continue;
dfs1(x,cur);
if(down[x][0]-edge[i].val>0)
{
int t=down[cur][1];
if(down[x][1]-2*edge[i].val>=0)t-=down[x][1]-2*edge[i].val;
t+=down[x][0]-edge[i].val;
if(t>=down[cur][0])
{
max2[cur]=max1[cur];
temp[cur]=down[cur][0];
down[cur][0]=t;
max1[cur]=x;
}
else if(t>temp[cur])
{
max2[cur]=x;
temp[cur]=t;
}
}
}
} void dfs2(int cur,int fa,int va)
{
up[cur][1]=0;
if(down[cur][1]-2*va>=0)up[cur][1]=max(up[cur][1],down[fa][1]-down[cur][1]+2*va+up[fa][1]-2*va);
else up[cur][1]=max(up[cur][1],down[fa][1]+up[fa][1]-2*va); up[cur][0]=0;
if(max1[fa]==cur)
{
int t=down[fa][0]-down[cur][0]+va;
up[cur][0]=max(up[cur][0],t+up[fa][0]-va);
int r=max2[fa];
if(down[r][1]-2*cost[r]>0)t=t-down[r][1]+2*cost[r];
t+=down[r][0]-cost[r];
up[cur][0]=max(up[cur][0],t+up[fa][1]-va);
}
else
{ int t=down[fa][0];
if(down[cur][1]-2*va>0)t-=down[cur][1]-2*va;
up[cur][0]=max(up[cur][0],t+up[fa][1]-va);
t=down[fa][1];
if(down[cur][1]-2*va>0)t-=down[cur][1]-2*va;
up[cur][0]=max(up[cur][0],t+up[fa][0]-va);
}
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int x=edge[i].to;
if(x==fa)continue;
dfs2(x,cur,edge[i].val);
}
} int main()
{
int t,Case=0;
read(t);
while(t--)
{
read(n);Init();
For(i,1,n)read(a[i]);
int u,v,w;
For(i,1,n-1)
{
read(u);read(v);read(w);
add_edge(u,v,w);
add_edge(v,u,w);
}
dfs(1,0,0);
dfs1(1,0);
dfs2(1,0,0);
printf("Case #%d:\n",++Case);
for(int i=1;i<=n;i++)printf("%d\n",max(down[i][0]+up[i][1],down[i][1]+up[i][0]));
}
return 0;
}
hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)的更多相关文章
- 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 ...
- HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)
http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...
- 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
- 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+
- HDU 5834 Magic boy Bi Luo with his excited tree
树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...
- HDU5834 Magic boy Bi Luo with his excited tree (树形DP)
题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...
- HDU5834Magic boy Bi Luo with his excited tree 树形dp
分析:典型的两遍dfs树形dp,先统计到子树的,再统计从祖先来的,dp[i][0]代表从从子树回来的最大值,dp[i][1]代表不回来,id[i]记录从i开始到哪不回来 吐槽:赛场上想到了状态,但是不 ...
- 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 ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
随机推荐
- mysql 64 zip download
open the url :: http://dev.mysql.com/downloads/file/?id=461109 and click the location "no tha ...
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— 基本功能演示(二)
目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 基本功能演示(二) 非常抱歉这个月实在太忙,一直 ...
- tomcat下bin文件夹下shell文件分析
在bin下面有9个sh文件,本文将逐步分析,今天就以version.sh为例 os400=false #uname取操作系统名称 如Linux 如果为OS400的操作系统 特殊处理 case &quo ...
- inner Join on 随随随随随便一记
幼儿园大班生(随便的记一记) JOIN 分为:内连接(INNER JOIN).外连接(OUTER JOIN).其中,外连接分为:左外连接( ...
- IOS6学习笔记(四)
1.GCD设置一个timer计时器 - (void)awakeFromNib { __weak id weakSelf = self; double delayInSeconds = 0.25; _t ...
- RHEL7软件包管理
本文介绍RHEL7的软件包管理 RHEL7下主要有RPM和YUM这两种包管理: YUM使用简单但需要联网,YUM会去网上的YUM包源去获取所需要的软件包并获取该包依赖的其他包 RPM的需要的操作精度比 ...
- How to Get SharePoint Client Context in SharePoint Apps (Provider Hosted / SharePoint Access ) in CSOM (Client Side Object Model)
http://www.codeproject.com/Articles/581060/HowplustoplusGetplusSharePointplusClientplusContex Downlo ...
- 打造高仿QQ的友盟反馈界面(MVP模式)
什么是MVP呢,简单来说就是将view层和逻辑完全独立出来,让逻辑和显示完全独立.本例中就是采用了这种模式,让activity作为view层,activity中涉及了适配器,所以这里尝试让适配器作为P ...
- 揭开智能配置上网(微信Airkiss)的神秘面纱
本文介绍微信利用Airkiss技术对wifi设备进行智能配置上网的场景,并分析其实现的原理.这里再次说明,Airkiss只是用于配置上网,其跟微信硬件平台的通信流程和接入协议规范完全没有关系.一个wi ...
- MFC Grid control 2.27
原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...