HDU 5834 Magic boy Bi Luo with his excited tree
树形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的更多相关文章
- 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
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
- HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)
http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...
- 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+
- 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 ...
- 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: ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
- 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 ...
- hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】
题目链接 hdu5834 题解 思路很粗犷,实现很难受 设\(f[i][0|1]\)表示向子树走回来或不回来的最大收益 设\(g[i][0|1]\)表示向父亲走走回来或不回来的最大收益 再设\(h[i ...
随机推荐
- linux服务器开发二(系统编程)--线程相关
线程概念 什么是线程 LWP:Light Weight Process,轻量级的进程,本质仍是进程(在Linux环境下). 进程:独立地址空间,拥有PCB. 线程:也有PCB,但没有独立的地址空间(共 ...
- FineReport中以jws方式调用WebService数据源方案
在使用WebService作为项目的数据源时,希望报表中也是直接调用这个WebService数据源,而不是定义数据连接调用对应的数据库表,这样要怎么实现呢? 在程序中访问WebService应用服务, ...
- U3D简单得换装技术
四个类完成,前提是 资源得名字配合 UI按钮点击响应类 using UnityEngine; using System.Collections; public class ButtonClickHan ...
- JS函数调用
function SayHello(word) { console.log(word); } function execute(Somefunction,value) { Somefunc ...
- 取汉子拼音首字母的C#和VB.Net方法
转载http://blog.fwhyy.com/2012/03/take-the-first-initials-method-of-csharp-and-vbnet/
- lcd_1602
module lcd_pdf( clk,lcd_data, lcd_rw,lcd_rs, lcd_en,rst_n ); input clk; input rst_n; output reg [:] ...
- idea不能加载xml
- 自定义dialog自动弹出软键盘
1.解决无法弹出输入法: 在show()方法调用之前,用dialog.setView(new EditText(context))添加一个空的EditText,由于是自定义的AlertDialog,有 ...
- 连接pgsql
package com.jpzhutech.select; import java.sql.Connection; import java.sql.DriverManager; import java ...
- 跑github上的Symfony项目遇到的问题
Loading composer repositories with package information Installing dependencies (including require-de ...