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. mongodb的安装使用和pymongo基本使用

    (1) mongodb的安装 下载tgz解压后,需要添加相应的环境变量才能在终端直接启动mongod. mongodb数据存储在/data/db中,需要手动创建目录树,同时mongod执行的时候如果权 ...

  2. SHA和MD5的Salt

    常用的对称加密算法有:DES.3DES.RC2.RC4.AES 常用的非对称加密算法有:RSA.DSA.ECC 使用单向散列函数的加密算法(摘要算法):MD5.SHA 那么什么是salt?生成一个随机 ...

  3. Hadoop:eclipse配置hadoop-eclipse-plugin(版本hadoop2.7.3)

    配置hadoop-eclipse-plugin(版本hadoop2.7.3): 1:首先下载我们需要的  hadoop-eclipse-plugin-2.7.3.jar,winutils.exe 和 ...

  4. React Native常用组件之ListView组件

    学习iOS开发的同学应该都知道UITableView,几乎每个APP中都有它的存在,而且衍生出各种形态:那么同样,ListView就是在React Native中的tableView,而且更加简单和灵 ...

  5. java中最常用jar包的用途

    jar包用途 axis.jarSOAP引擎包commons-discovery-0.2.jar用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期管理的常用方法.jaxrpc.jar ...

  6. 智能DNS

    DNS查找下一个服务的地址   一.智能DNS APP通过域名访问DNS服务器,DNS根据域名对应一组IP中随机选择一个,发给APP.从这个意义说智能DNS,智能DNS相当一个七层的负载均衡. 二.H ...

  7. dpdk对虚拟化的支持调研

    目录: 虚拟化 dpdk的实现研究 virtio vhost SR-IOV 热迁移相关 研究拓展 本文记录近期对dpdk在虚拟化和云计算领域应用的研究成果,内容梳理如下. 虚拟化 虚拟化,抽象来说,就 ...

  8. 使用SpringMVC报错 Error creating bean with name 'conversionService' defined in class path resource [springmvc.xml]

    使用SpringMVC报错 Error creating bean with name 'conversionService' defined in class path resource [spri ...

  9. colorlog的三个例子

    例1:默认的log_colors import logging from logging.handlers import RotatingFileHandler from colorlog impor ...

  10. APP埋点:页面统计与事件统计该如何入手?

    我们平时所说的埋点,可以大致分为两部分,一部分是统计APP页面访问情况,即页面统计:另外一部分是统计APP内的操作行为,及自定义事件统计. 一.页面统计 页面统计,可以统计应用内各个页面的访问次数(P ...