HDU 6201transaction 【树形DP】
来源:互联网 发布:瑞典 二战 知乎 编辑:程序博客网 时间:2024/06/06 00:46
2017 ACM/ICPC Asia Regional Shenyang Online
HDU 6201transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell.
As we know, the price of this book was different in each city. It isai yuan in i t city. Kelukin will take taxi, whose price is 1 yuan per km and this fare cannot be ignored.
There aren−1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
As we know, the price of this book was different in each city. It is
There are
Input
The first line contains an integer T (1≤T≤10 ) , the number of test cases.
For each test case:
first line contains an integern (2≤n≤100000 ) means the number of cities;
second line containsn numbers, the i th number means the prices in i th city; (1≤Price≤10000)
then followsn−1 lines, each contains three numbers x ,y and z which means there exists a road between x and y , the distance is z km (1≤z≤1000) .
For each test case:
first line contains an integer
second line contains
then follows
Output
For each test case, output a single number in a line: the maximum money he can get.
Sample Input
1 4 10 40 15 30 1 2 301 3 23 4 10
Sample Output
8
题意:给一棵树,树点有权值(书价格),树边是消耗,从任意一个点买一本树,卖到另一个点,求最大利益。
思路:树形DP,用dp[x][0],表示在x的子树买一本的获得最大权值(为负的),dp[x][1]表示在x的子树卖一本书所获得的最大权值。答案就是在每一个节点的买卖书,注意可以不买也不卖获利为零。
ACcode
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;typedef long long LL;const int N=100005;LL dp[N][2],ans;int val[N],n,m;struct edge{ int to,cost; edge(){} edge(int x,int y) { to=x; cost=y; }};vector<edge>g[N];void dfs(int x,int fa){ dp[x][0]=-val[x]; dp[x][1]=val[x]; for(int i=0;i<g[x].size();i++) { int y=g[x][i].to; if(y==fa)continue; dfs(y,x); dp[x][0]=max(dp[x][0],dp[y][0]-g[x][i].cost); dp[x][1]=max(dp[x][1],dp[y][1]-g[x][i].cost); } ans=max(ans,dp[x][0]+dp[x][1]);}int main(){ int TA,x,y,z; scanf("%d",&TA); while(TA--) { scanf("%d",&n); for(int i=1;i<=n;i++)g[i].clear(); for(int i=1;i<=n;i++) scanf("%d",&val[i]); for(int i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&z); g[x].push_back(edge(y,z)); g[y].push_back(edge(x,z)); } ans=0; dfs(1,-1); printf("%lld\n",ans); } return 0;}
阅读全文
1 0
- HDU-6201 transaction transaction transaction(树形dp)
- hdu 6201 transaction transaction transaction 树形dp
- HDU 6201transaction 【树形DP】
- HDU 6201 transaction transaction transaction【树形DP||SPFA最长路】
- HDU 6201 transaction transaction transaction(树形dp)
- HDU 6201 transaction transaction transaction (树形dp)
- 2017沈阳网络赛 1008 HDU 6201 transaction transaction transaction(树形dp)
- HDU 6201 transaction transaction transaction (树形DP or 拆点最短路)
- Hdu 6201 transaction transaction transaction 树型DP
- HDU 6201 transaction transaction transaction(dp)
- hdu 6201 transaction transaction transaction tree dp
- HDU 6201 transaction transaction transaction dp
- HDU6201 2017 transaction transaction transaction 树形DP?
- HDU沈阳网络赛:transaction transaction transaction(树形dp & 最短路)
- 树形DP HDU 6201
- hdu 6201 树形DP
- HDU 6201 树形DP
- HDU 6201 树形dp
- 命令行java可以运行,javac不能运行
- spring boot入门demo
- POJ 1862
- Java开发环境配置的作用
- (网易2018校招笔试)[编程题] 游历魔法王国
- HDU 6201transaction 【树形DP】
- Jmeter FTP服务
- 全面升级Android面试之HandlerThread面试题集
- jmeter访问本地搭建的服务器报错的问题
- SSH命令与ssh免密登陆
- luoguP2709 小B的询问
- 2017 ACM/ICPC Asia Regional Shenyang Online
- Shiro过滤器过滤属性含义
- Servlet的页面跳转方式