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


Problem Description
Bi Luo is a magic boy, he also has a migic tree, the tree has N nodes , in each node , there is a treasure, it's value is V[i], and for each edge, there is a cost C[i], which means every time you pass the edge i , you need to pay C[i].

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?

 

Input
First line is a positive integer T(T104) , represents there are T test cases.

Four each test:

The first line contain an integer N(N105).

The next line contains N integers V[i], which means the treasure’s value of node i(1V[i]104).

For the next N1 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(1c104).

You can assume that the sum of N will not exceed 106.

 

Output
For the i-th test case , first output Case #i: in a single line , then output N lines , for the i-th line , output ans[i] in a single line.

 

Sample Input
1
5
4 1 7 7 7
1 2 6
1 3 1
2 4 8
3 5 2

 

Sample Output
Case #1:
15
10
14
9
15
题意:
每个节点有价值v[i]的宝物,但是任何两个节点u,v之间的路走一次花费为w,从每个节点出发最多可以赚多少钱;
思路:
树形dp的题目,需要记录转移的最大和次大,注意转移的情况,不能写漏了;
AC代码:
#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;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5856654.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/373933.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java EE过去,现在和云7

最近的JavaOne 2011的一个突出主题是下一个主要的Java EE 7版本。 正如主题发言中所述&#xff0c;有关工作正在进行中。 它将包含我们已经从先行者那里知道的28个规范以及一些新规范。 没人可以告诉您确切的号码&#xff0c;因为EE 7仅在“及时”完成时才会接受新的规范。 这意…

python cnn识别图像_笨方法学习CNN图像识别(一)—— 图片预处理

— 全文阅读5分钟 —在本文中&#xff0c;你将学习到以下内容&#xff1a;通过数据增强增加样本量调整图片大小便于网络训练前言图像识别的准备工作就是要对我们拿到手的样本图片进行预处理&#xff0c;具体就是数据增强和调整图片大小&#xff0c;这些准备工作都是为训练网络做…

随机数发生器

很多人喜欢用 rand()%n产生区间 [0,n]内的一个随机整数。姑且不论这样产生的整数是否仍然均匀分布&#xff0c;当 n大于 RAND_MAX 时&#xff0c;此法并不能得到期望的结果。由于RAND_MAX 很可能只是32767这么小&#xff0c;在使用此法时应当小心。 #include "stdio.h&quo…

Request和Response详解

转自&#xff1a;http://zhidao.baidu.com/link?url8BI0cjlcFdBSJKHTZlpo874eqtbTJoZfrh3miQgM_05RvSER8skPiBc1wSPZtXT8OGGCHfVXFAzAosa6E5HBl_ 内置对象request&#xff1a;请求对象request.getParameter("名字") 获得客户端输入的信息***************request.get…

将Maven与Ivy集成

问题是&#xff1a;您在Ivy存储库中&#xff08;只有那里&#xff09;有一些资源&#xff0c;您想在基于Maven的项目中使用这些资源。 可能的解决方案&#xff1a; 由于Ivy可以轻松使用Maven风格的存储库&#xff08;因此&#xff0c;您的Ivy客户端可以继续使用Ivy并进行一些微…

用python下载辞典

用python下载词源词典Etymoline Online Etymology Dictionary是最好的 English 词源词典&#xff0c;现在来说没有之一。但是&#xff0c;一直在PC上查单词有时不是很方便&#xff0c;遂就想怎么才能在手机上使用。现在的手机上的词典&#xff0c;除了BlueDict、MDict之外&…

程序员都用什么来记录知识_1年前的小五都用 Python 来做什么?

↑ 点击上方 “凹凸数据” 关注 星标 ~ 每天更新&#xff0c;干货不断 (多图预警)注&#xff1a;这是小五一年前在知乎的回答&#xff0c;当时还只有凹凸数读一个公众号&#xff0c;所以很多图片都会带有数读或者知乎的水印。作为一个菜鸟数据分析师&#xff0c;只会sqlpytho…

CSDN编程挑战——《高斯公式》

高斯公式 题目详情: 高斯在上小学时发明了等差数列求和公式:12..1005050。现在问题在于给你一个正整数n&#xff0c;问你他可以表示为多少种连续正整数之和&#xff1f;&#xff08;自身也算&#xff09;。 输入格式&#xff1a; 多组数据&#xff0c;每组数据一行&#xff0c…

SQL-行转列(PIVOT)实例1

--未旋转之前的查询结果 select s.Name ShiftName,h.BusinessEntityID,d.Name as DpartmentName from HumanResources.EmployeeDepartmentHistory h inner join HumanResources.Department d on h.DepartmentIDd.DepartmentIDinner join HumanResources.Shift s on s.ShiftIDh…

将MongoDB与Morphia结合使用

在过去的几年中&#xff0c; NoSQL数据库&#xff08;例如CouchDB&#xff0c;Cassandra和MongoDB&#xff09;在不需要运行传统RDBMS的语义和开销的应用程序中得到了普及。 我不会进入选择NoSQL数据库的设计决策&#xff0c;因为其他人已经做得很好&#xff0c;但是我将结合我…

webservice接口_webservice服务器端发票识别接口

关键词&#xff1a;发票识别 私有云发票识别 发票识别API接口 webservice发票识别平台发票&#xff0c;一个再也熟悉不过的财务往来凭证&#xff0c;录入发票&#xff0c;一项让多少财会人员头疼的工作。过去录入一张发票需要一个财会人员5分钟的时间&#xff0c;那么这个人在工…

二叉树学习——简单入门题

入门题一&#xff1a; 输入一颗二叉树&#xff0c;你的任务是按从上到下、从左到右的顺序输出各个节点的值。每个节点都按照从根节点到它的移动序列给出 &#xff08;L表示左&#xff0c;R表示右&#xff09;。在输入中&#xff0c;每个节点的左括号和右括号之间没有空格&#…

java8-4 多态的练习以及题目

1、/* 多态练习&#xff1a;猫狗案例*/ 1 class Animal {2 public void eat(){3 System.out.println("吃饭");4 }5 }6 7 class Dog extends Animal {8 public void eat() {9 System.out.println("狗吃肉"); 10 } 11 12 public void lookDoor() { 13 Syste…

一个简单的socket通信小demo

写了一个socket的程序&#xff0c;可以和本地的服务器进行通信&#xff0c;要先和服务器建立链接&#xff0c;然后发送登录信息&#xff0c;验证成功&#xff0c;就可以和服务器通信了 1 页面截图 2 点击链接服务器&#xff0c;可以链接服务器&#xff0c;服务器的ip地址为&…

Java并发教程– CountDownLatch

Java中的某些并发实用程序自然会比其他并发实用程序受到更多关注&#xff0c;因为它们可以解决通用问题而不是更具体的问题。 我们大多数人经常遇到执行程序服务和并发集合之类的事情。 其他实用程序不太常见&#xff0c;因此有时它们可​​能会使我们逃脱&#xff0c;但是请记…

汉仪尚巍手书可以商用吗_【商用车维修】夏天修空调可以撑起全年修车收入的一半,你会了吗?...

更多精彩&#xff0c;请点击上方蓝字关注我们&#xff01;车载空调是炎热的季节必不可少的利器&#xff0c;但用得多&#xff0c;毛病也多了起来&#xff0c;今天和大家分享一些空调系统的相关知识&#xff0c;助力修车师傅们来应对空调系统的相关故障问题。如何判断制冷系统的…

CSDN编程挑战——《-3+1》

-31 题目详情: 有一个数列&#xff0c;所有的数都是非负整数&#xff0c;你可以进行如下方式进行一次操作&#xff08;注意一次完整的操作必须先后完成如下两个步骤&#xff09;&#xff1a; &#xff08;1&#xff09; 任选一个不小于3的数&#xff0c;把它减少3。 &#xff…

游戏感悟

1.所谓游戏平衡&#xff0c;就是指玩家没有最优解。 2.所谓公司的文化&#xff0c;就是指员工被公司洗脑的那些观点(认知)。 3.人是能动的&#xff0c;摆脱平庸。转载于:https://www.cnblogs.com/yangzhou33/p/5074509.html

Git 简单使用

1.Git是什么 简介&#xff1a;Git是 Linux 之父 Linus Trovalds&#xff0c;为管理 Linux 内核代码而建立的&#xff0c;被认为是分布式版本控制工具中的顶级水准。智能、友好、强健、高效。 作用&#xff1a;新建一个分支&#xff0c;把服务器上最新版的代码fetch下来&#x…

Vaadin附加组件和Maven

介绍 我喜欢Vaadin的 &#xff08;众多&#xff09;一件事是它对Vaadin框架的“附加组件”社区-他们称之为Vaadin目录 。 “附加组件”是框架中社区贡献的附加组件&#xff0c;可以是任何东西&#xff0c;例如从新的客户端小部件到数据表的延迟加载容器。 我肯定会为Activiti看…