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. Hive2.2.1安装使用

    解压缩hive安装包tar zxvf apache-hive-2.1.1-bin.tar.gz 安装mysqlsudo yum install mysql-server 安装 mysql connec ...

  2. datagrid 用法

    http://blog.csdn.net/xhhuang1979/article/details/8331682

  3. get app id

    Install Download the Web AppBuilder for ArcGIS (Developer Edition) ZIP file to your local drive and ...

  4. 并查集 试水 hdu1232

    #include <stdio.h> #include <stdlib.h> int n,m; ],rank[]; int count; int find(int x) { i ...

  5. NOIP 统计单词个数

    描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包含的单词个 ...

  6. curator的版本兼容问题(需注意)

    Curator 存在版本兼容问题. Curator 2.x.x-兼容两个zk 3.4.x 和zk 3.5.x, Curator 3.x.x-兼容兼容zk 3.5. Versions The are c ...

  7. codeforces 703B

    题意:有n座城市,其中k座是省会城市,每个城市有对应的点权,城市1-2-3-...-n-1有一条路相连,省会城市与其他所有的城市相连,且每两个城市间最多有一条路,每条路的边权为路连接的两座城市的点权乘 ...

  8. 内核hlist的使用

    struct hlist_head { struct hlist_node *first; }; struct hlist_node { struct hlist_node *next, **ppre ...

  9. SSH2 增删查改实例

    (一)引入包 (共73个,不一定都需要,但是我的项目是这么多,经过调试,没有包冲突) (二)创建数据库表 建立数据库octtest,并创建user表,表里面一共4个字段:id,姓,名,年龄. 语句如下 ...

  10. LinkedBlockingQueue 与ConcurrentLinkedQueue队列的不同与同

    LinkedBlockingQueue 的API中,从队列中获取元素,有以下几个方法: 1.take():原文:Retrieves and removes the head of this queue ...