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. leetcode每日一题——反转整数

    题目: 反转整数 难度: 简单 描述: 给定一个 32 位有符号整数,将整数中的数字进行反转. 解法: class Solution { public int reverse(int x) { //i ...

  2. nodejs入门-静态文件服务器

    本文展示是基于node.js的静态文件服务器,代码参考自这里,主要是练习node http.文件模块的使用,另外,对理解http协议也很有帮助除了实现了基本的路由控制,还实现了MIME类型.304缓存 ...

  3. JavaScript传递的是引用的副本

    看例子 var test1 = function (args) { args.name = "lcc2"; args = { name: "lcc3" }; } ...

  4. c++的格式控制

    1: 每个iostream对象维持一个控制IO格式化细节的格式状态.标准库定义了一组操纵符来修改对象的格式状态.所谓操纵符是可用作输入或输出操作符的函数或对象.iostream和iomanip头文件中 ...

  5. c# 布局(stackpanel)

    <Grid> <StackPanel> <Button Content="asas"> </Button> <Button C ...

  6. poj 3253超时

    哈夫曼树超时是因为每次都需要重排,十分浪费时间,即使是快排.

  7. spring security采用自定义登录页和退出功能

    更新... 首先采用的是XML配置方式,请先查看  初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...

  8. Which HTTP methods match up to which CRUD methods?

    https://stackoverflow.com/questions/6203231/which-http-methods-match-up-to-which-crud-methods   Crea ...

  9. flume-ng源码阅读memory-channel(原创)

    org.apache.flume.channel.MemoryChannel类是Flume-NG的memory-channel. private LinkedBlockingDeque<Even ...

  10. css tips —— 神奇的max-width,min-width, width覆盖规则

    max-width在比width小时,即使width使用!important来加权,仍会max-width生效: max-width比min-width小时,width < min-width, ...