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 a i  ai

yuan yuan

in i i

t t

city. Kelukin will take taxi, whose price is 1 1

yuan yuan

per km and this fare cannot be ignored.
There are n−1 n−1

roads connecting n n

cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.

InputThe first line contains an integer T T

(1≤T≤10 1≤T≤10

) , the number of test cases.
For each test case:
first line contains an integer n n

(2≤n≤100000 2≤n≤100000

) means the number of cities;
second line contains n n

numbers, the i i

th th

number means the prices in i i

th th

city; (1≤Price≤10000) (1≤Price≤10000)

then follows n−1 n−1

lines, each contains three numbers x x

, y y

and z z

which means there exists a road between x x

and y y

, the distance is z z

km km

(1≤z≤1000) (1≤z≤1000)

.
OutputFor 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 30
1 3 2
3 4 10

Sample Output

8

题意:现在有一棵树,每个节点有自己的价格,边之间有运费,问最大差价是多少。

思路:可以树DP。这里用的最长路,没想到啊。   每个点与源点连一个正价,与汇点连一个负价。然后跑最长路,就可以得到最大收益。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int inf=1e9+;
const int maxn=;
int S,T,a[maxn],Laxt[maxn],Next[maxn],To[maxn],Len[maxn],dis[maxn],in[maxn],cnt;
void add(int u,int v,int w){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; Len[cnt]=w;
}
void read(int &x){
x=; char c=getchar();
while(c>''||c<'') c=getchar();
while(c<=''&&c>='') x=x*+c-'',c=getchar();
}
void SPFA()
{
rep(i,,T) dis[i]=-inf,in[i]=;
dis[S]=; queue<int>q;
q.push(S); in[S]=;
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(dis[u]+Len[i]>dis[v]) {
dis[v]=dis[u]+Len[i];
if(!in[v]) in[v]=,q.push(v);
}
}in[u]=;
}
}
int main()
{
int Case,N,u,v,w;
scanf("%d",&Case);
while(Case--){
read(N); T=N+;
rep(i,,N) read(a[i]);
rep(i,,T) Laxt[i]=; cnt=;
rep(i,,N-){
read(u); read(v); read(w);
add(u,v,-w); add(v,u,-w);
}
rep(i,,N) add(S,i,-a[i]);
rep(i,,N) add(i,T,a[i]);
SPFA();
printf("%d\n",dis[T]);
}
return ;
}

数据比较奇葩,普通的SPFA效率比优先队列的高。。。

#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int inf=1e9+;
const int maxn=;
int S,T,a[maxn],Laxt[maxn],Next[maxn],To[maxn],Len[maxn],dis[maxn],cnt;
void add(int u,int v,int w){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; Len[cnt]=w;
}
void SPFA()
{
rep(i,,T) dis[i]=-inf; dis[S]=;
priority_queue<pii>q;
q.push(mp(-,S));
while(!q.empty()){
int u=q.top().second; q.pop();
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(dis[u]+Len[i]>dis[v]) {
dis[v]=dis[u]+Len[i];
q.push(mp(-dis[v],v));
}
}
}
}
int main()
{
int C,N,u,v,w;
scanf("%d",&C);
while(C--){
scanf("%d",&N); T=N+;
rep(i,,N) scanf("%d",&a[i]);
rep(i,,T) Laxt[i]=; cnt=;
rep(i,,N-){
scanf("%d%d%d",&u,&v,&w);
add(u,v,-w); add(v,u,-w);
}
rep(i,,N) add(S,i,a[i]);
rep(i,,N) add(i,T,-a[i]);
SPFA();
printf("%d\n",dis[T]);
}
return ;
}

树DP,Mn表示经过这个点到子树里买的最小值,Mx表示经过这个点到子树里卖的最大值,每次上传时由于多经过一条边,减去边权更新即可:

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Mx[maxn],Mn[maxn];
int Laxt[maxn],Next[maxn],To[maxn],Len[maxn],cnt,ans;
void add(int u,int v,int w){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; Len[cnt]=w;
}
void dfs(int u,int fa){
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=fa) {
dfs(v,u);
Mn[u]=min(Mn[v]+Len[i],Mn[u]);
Mx[u]=max(Mx[v]-Len[i],Mx[u]);
}
}
ans=max(Mx[u]-Mn[u],ans);
}
int main()
{
int T,N,u,v,w;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
rep(i,,N) Laxt[i]=;
cnt=; ans=;
rep(i,,N) scanf("%d",&Mn[i]),Mx[i]=Mn[i];
rep(i,,N-){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
}
dfs(,);
printf("%d\n",ans);
}
return ;
}

HDU - 6201:transaction transaction transaction(最长路)的更多相关文章

  1. [HDU 1317]XYZZY[SPFA变形][最长路]

    题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...

  2. HDU 6201 transaction transaction transaction(拆点最长路)

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  3. hdu 6201 transaction (最短路变形——带负权最长路)

    题意: 给定n个城市的货物买卖价格, 然后给定n-1条道路,每条路有不同的路费, 求出从某两个城市买卖一次的最大利润. 利润 = 卖价 - (买价 + 路费) 样例数据, 最近是从第一个点买入, 第4 ...

  4. HDU - 6201 transaction transaction transaction(spfa求最长路)

    题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...

  5. hdu 6501 transaction transaction transaction 最长路/树形DP/网络流

    最长路: 设置一个虚拟起点和虚拟终点,每个点与起点间一条负边,值为这个点书的价值的相反数(代表买书花钱),每个点与终点连一条正边,值为这个点的书的价格(代表卖书赚钱). 然后按照图中给的边建无向边,权 ...

  6. hdu 6201 transaction transaction transaction

    https://vjudge.net/contest/184514#problem/H 题意: 一个商人为了赚钱,在城市之间倒卖商品.有n个城市,每个城市之间有且只有一条无向边连通.给出n个城市的货物 ...

  7. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  8. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  9. hdu 4123 树的最长路+RMQ

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. linux---(6/27)tr命令和sed命令详解

    Tr命令: tr是简单的单个“字符”处理工具,而sed是功能非常强大的“字符串”处理工具. 用于查询,字符串2用于处理各种转换.tr刚执行时,字符串1中的字符被映射到字符串2中的字符,然后转换操作开始 ...

  2. lock关键字的用法

    一直以来对于lock关键字的用法都存有疑惑,也从网上看到很多关于他的资料包括MSDN,无奈MSDN讲述的真是让人上火.今天决定小小研究一下 一直都知道lock是锁定某一变量从而实现对某一代码段的独占执 ...

  3. 【leetcode刷题笔记】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. var和let/const的区别

    let和const是 ES6 新增的命令,用于声明变量,这两个命令跟 ES5 的var有许多不同,并且let和const也有一些细微的不同,再认真阅读了阮一峰老师的文档后,发现还是有一些不知道的细节. ...

  5. Java套接字socket编程笔记

    相对于C和C++来说,Java中的socket编程是比较简单的,比较多的细节都已经被封装好了,每次创建socket连接只需要知道地址和端口即可. 在了解socket编程之前,我们先来了解一下读写数据的 ...

  6. Android Studio 入门级教程

    引用原文:http://www.cnblogs.com/abao0/p/6934023.html 写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权.希望尽自己的努力 ...

  7. C++ 之虚函数的实现原理

    c++的多态使用虚函数实现,通过“晚绑定”,使程序在运行的时候,根据对象的类型去执行对应的虚函数. C++ 之虚函数的实现原理 带有虚函数的类,编译器会为其额外分配一个虚函数表,里面记录的使虚函数的地 ...

  8. SSO 证书配置

    ssodev.crt为开发环境证书ssotest.crt为测试环境证书 将证书拷贝到目录:{JDK}\jre\lib\security 其中 {JDK} 是实际安装JDK的位置.然后cmd进入命令窗口 ...

  9. Find Min In Rotated Sorted Array,寻找反转序列中最小的元素。

    问题描述:寻找反转序列中最小的元素. 算法分析:和寻找某个数是一个道理,还是利用二分查找,总体上分两种情况.nums[left]<=nums[mid],else.但是,在截取子序列的时候,有可能 ...

  10. 分享:SQL优化器简介

    SQL优化是我们经常会遇到的问题,无论你是专职的数据分析人员还是全栈开发大神或者是CURD搬运工. 我们在工作中经常会听到这样的声音:“查询慢?加个索引吧”.虽然加索引并不一定能解决问题,但是这体现了 ...