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. OpenCL 学习step by step (5) 使用二维NDRange workgroup

    http://www.cnblogs.com/mikewolf2002/archive/2012/09/07/2675634.html 在本教程中,我们使用二维NDRange来设置workgroup, ...

  2. CSS3鼠标悬停8种动画特效

    在线演示 本地下载

  3. Python 面向对象的三大特性:封装,继承,多态

    # 面向对象的三大特性:封装,继承,多态 # 继承的影响:资源的继承,资源的使用,资源的覆盖,资源的累加 # 资源的继承,在Python中的继承是指能使用父类的资源,而不是说在子类也复制一份父类代码到 ...

  4. 防盗链&CSRF&API接口幂等性设计

    防盗链技术 CSRF(模拟请求) 分析防止伪造Token请求攻击 互联网API接口幂等性设计 忘记密码漏洞分析 1.Http请求防盗链 什么是防盗链 比如A网站有一张图片,被B网站直接通过img标签属 ...

  5. share point CSOM 客户端模式 创建 list

    /// <summary> /// 创建新的列表 list /// </summary> /// <param name="web"></ ...

  6. CocoaPods学习系列3——创建和使用私有Pods

    前一篇记录了使自己的项目支持CocoaPods管理的过程,核心的步骤就是podspec的配置和提交.这个文件,记录了类库的详细信息,用于对类库的集成. 需要注意的一点,上一篇创建的podspec文件, ...

  7. java 类的加载、连接和初始化

    JVM和类 调用Java命令运行Java程序时,该命令将会启动一条Java虚拟机进程,不管该Java程序启动了多少条线程,创建了多少个变量,它们都处于该Java虚拟机进程里,共享该JVM进程的内存区. ...

  8. SkyDNS试用

    SkyDNS试用 简介 SkyDNS是kubernetes用于服务发现的默认的开源DNS服务.本文将抛开kubernetes单独体验SkyDNS.其开源在github.依赖与etcd作为数据存储. 其 ...

  9. python学习笔记(SMTP邮件发送)

    想着给框架添加邮件发送功能.所以整理下python下邮件发送功能 首先python是支持邮件的发送.内置smtp库.支持发送纯文本.HTML及添加附件的邮件 之后是邮箱.像163.qq.新浪等邮箱默认 ...

  10. 调用http接口的工具类

    网上面有很多,但是我们项目怎么也调不到结果,试了差不多很多案例,都是报connection reset 后来,我发现是有一个验证,需要跳过验证.然后才能调接口.所以找了一个忽略https的方法.进行改 ...