【2017沈阳网络赛】1008 hdu6201 transaction transaction transaction 树形dp
来源:互联网 发布:wpf数据绑定详解 编辑:程序博客网 时间:2024/05/22 05:12
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
给出一棵树,每个点都有点权,问取两个点,使得T-S-sumw为最大(T为终点的点权,S为起点的点权,sumw为S到T的路径长度),可取相同的点。
思路:
树形dp,设d[t][0]为节点t的最大值,d[t][1]为最小值。
//// main.cpp// 1008//// Created by zc on 2017/9/10.// Copyright © 2017年 zc. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#define ll long longusing namespace std;const int N=110000;int n,a[N],d[N][2],ans;vector<pair<int,int> >r[N];void dfs(int t,int fa){ int mmax=a[t],mmin=a[t]; for(int i=0;i<r[t].size();i++) { int j=r[t][i].first; if(j==fa) continue; dfs(j,t); if(mmax<d[j][0]-r[t][i].second) mmax=d[j][0]-r[t][i].second; if(mmin>d[j][1]+r[t][i].second) mmin=d[j][1]+r[t][i].second; } ans=max(ans,mmax-mmin); d[t][0]=mmax; d[t][1]=mmin;}int main(int argc, const char * argv[]) { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) r[i].clear(); for(int i=0;i<n-1;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); r[x].push_back(make_pair(y,z)); r[y].push_back(make_pair(x,z)); } ans=0; memset(d,0,sizeof(d)); dfs(1,-1); printf("%d\n",ans); }}
阅读全文
0 0
- 【2017沈阳网络赛】1008 hdu6201 transaction transaction transaction 树形dp
- HDU6201 2017 transaction transaction transaction 树形DP?
- 2017沈阳网络赛 1008 HDU 6201 transaction transaction transaction(树形dp)
- ICPC2017网络赛(沈阳)1008 transaction transaction transaction——树形DP
- HDU6201 | 2017 ACM-ICPC 亚洲区(沈阳赛区)网络赛-H transaction transaction transaction
- HDU6201 | 2017 ACM-ICPC 亚洲区(沈阳赛区)网络赛-H transaction transaction transaction
- HDU6201 | 2017 ACM-ICPC 亚洲区(沈阳赛区)网络赛-H transaction transaction transaction
- HDU沈阳网络赛:transaction transaction transaction(树形dp & 最短路)
- hdu6201 transaction transaction transaction 树形dp || 最短路
- HDU 6201 transaction transaction transaction &&沈阳网络赛1008
- HDU 6201 transaction transaction transaction (2017沈阳网络赛
- HDU 6201 transaction transaction transaction (2017沈阳网络赛
- hdu6201 transaction transaction transaction
- hdu6201 transaction transaction transaction
- HDU6201 transaction transaction transaction
- HDU6201-transaction transaction transaction
- HDU-6201 transaction transaction transaction(树形dp)
- hdu 6201 transaction transaction transaction 树形dp
- oracle11之Database links
- 如何理解Swift中Optional的!和?
- 在taskctl里做定时任务时,是用翻牌机制好还是用定时器好呢?
- Android网络数据解析之FastJson
- Thymeleaf教程 (一) 简介
- 【2017沈阳网络赛】1008 hdu6201 transaction transaction transaction 树形dp
- Centos7 联网和不联网 安装MySQL的方法
- BaseTask 任务分类
- webstorm设置文件类型
- Shader学习之Cg语言一(Cg语言概述)
- bootstrap-datepicker限定可选时间范围 一、应用场景 实际应用中可能会根据日期字段查询某个日期范围内的数据,则需要对日期选择器可选时间进行限制, 如:开始时间不能大于结束时间,
- 通过Fiddler肆意修改接口返回数据进行测试
- C#综合揭秘——细说多线程(下)
- 四旋翼飞行器教学笔记2.2——姿态读取的计算