Hdu 6201 transaction transaction transaction 树型DP

来源:互联网 发布:建材价格查询软件 编辑:程序博客网 时间:2024/05/18 12:28

transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 118    Accepted Submission(s): 52


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 is ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are n1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
 

Input
The first line contains an integer T (1T10) , the number of test cases. 
For each test case:
first line contains an integer n (2n100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1Price10000) 
then follows n1 lines, each contains three numbers xy and z which means there exists a road between x and y, the distance is zkm (1z1000)
 

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
 

Source
2017 ACM/ICPC Asia Regional Shenyang Online



求一棵树上的max(a[i]-a[j]-Dij), i!=j, 其中Dij表示 i , j 树上路径长度。


这是一道树型DP.

对于每个固定的 i , 我们发现:a[i]不变,只要使a[j]+Dij最小。

既然是树型DP,我们可以先只考虑某节点 i 和它的子树,从叶子向上DP。用dp[i]表示在某节点 i 卖出,在它和它的子树上买入的最小的a[j]+Dij.

如果 j 是 i 的儿子,那么所有 j 以下的点都必然经过 i 到 j 的路径,代价必然先加上这条边的长度。如果起点是 j, 代价加上a[j]即可;否则,由于我们已经求出 j 和它子树上的最优解,所以这些点到 i 的最优解代价是到 j 的最优解加上边长。

这样dfs一次,可以求出某点为终点,子树上买入的最优解。

对于某点的父节点以上的其他点,同样可以自上到下DP,过程与自上至下类似,把子节点换成父节点即可。再dfs一次,即可求出答案。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=100005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int dp[maxn],head[maxn],a[maxn],d[maxn];bool visit[maxn],f[maxn];int num,ans;struct Edge {int from,to,pre,dist;};Edge edge[maxn*2];void addedge(int from,int to,int dist) {edge[num]=(Edge){from,to,head[from],dist};head[from]=num++;edge[num]=(Edge){to,from,head[to],dist};head[to]=num++;}void dfs(int now) {visit[now]=1;dp[now]=inf;d[now]=inf;for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) {dfs(to);d[now]=min(d[now],edge[i].dist+d[to]);dp[now]=min(dp[now],min(dp[to],a[to])+edge[i].dist);}}if (d[now]==inf) d[now]=0;}void dfs2(int now,int fa,int dist) {visit[now]=1;if (fa!=-1)    dp[now]=min(dp[now],min(dp[fa]+dist,a[fa]+dist)); for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to])dfs2(to,now,edge[i].dist);}ans=max(ans,a[now]-dp[now]);}int main() {int cas;scanf("%d",&cas);while (cas--) {int n,i,x,y,d;scanf("%d",&n);num=0;memset(head,-1,sizeof(head));for (i=1;i<=n;i++) scanf("%d",&a[i]);for (i=1;i<n;i++) {scanf("%d%d%d",&x,&y,&d);addedge(x,y,d);}mem0(visit);dfs(1);ans=-inf;mem0(visit);dfs2(1,-1,0);printf("%d\n",ans);}return 0;}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 练扫踢胫骨旁边的肌肉受伤了怎么办 四个月宝宝没抱住摔了头部怎么办 老公老是跟年轻的小姑娘聊天怎么办 老婆出轨老公想离婚又舍不得怎么办 孕妇打完无痛分娩针就想睡觉怎么办 熟食店开空调菜品吹的很干怎么办 不锈钢锅在液化气烧了发黄怎么办 在小镇门面卤菜店不好卖怎么办? 被辣椒辣到嘴唇了该怎么办 沁园净水机不制水指示灯不亮怎么办 太辣了辣得胃疼怎么办 出现连接问题或mmi码无效怎么办 存折丢了怎么办卡号也不记得了 车内皮子被烂苹果腐蚀有印怎么办 锅被腐蚀后变黑色应该怎么办 后厨炉灶里的炉芯进水了怎么办 小儿九个月老是流黄鼻子该怎么办 肉炖的老了不烂怎么办 吃了凉东西现在一直打嗝应该怎么办 喝了很多水还是觉得口渴怎么办 刚买的猪肝没洗直接炒了怎么办 四个多月的宝宝吃了脏东西怎么办 狗吃了脏东西拉稀呕吐怎么办 五个月宝宝怕吃药导致奶不喝怎么办 蒸锅锅盖吸住了怎么办锅比锅盖要大 豇豆没熟孕妇吃了中毒怎么办 孩子积食拉不出粑粑憋的直哭怎么办 2岁宝宝总是半夜拉粑粑怎么办 金毛拉很臭的稀粑粑怎么办 点餐系统登录后没有菜单怎么办? 环亚在线微交易亏了钱怎么办 钢管舞报了教练班觉得学不会怎么办 微信上聊天被外国人给骗了该怎么办 微信冒充朋友骗走我钱怎么办 凉皮调料水鸡精味精放多了怎么办 吃了地屈孕酮后月经不干不净怎么办 藕片用热水炒后变色了怎么办? 外汇延期收款忘了报告了怎么办 怀孕不小心吃了马生菜怎么办 高压锅的皮圈很容易坏是怎么办 华为应用市场账号密码忘记了怎么办