Hdu 6201 transaction transaction transaction【最长路】

transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 519    Accepted Submission(s): 245


Problem Description
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  ai   yuan  in  i t  city. Kelukin will take taxi, whose price is  1 yuan  per km and this fare cannot be ignored.
There are  n1  roads connecting  n  cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
 

Input
The first line contains an integer  T  ( 1T10 ) , the number of test cases. 
For each test case:
first line contains an integer  n  ( 2n100000 ) means the number of cities;
second line contains  n  numbers, the  i th  number means the prices in  i th  city;  (1Price10000)  
then follows  n1  lines, each contains three numbers  x y  and  z  which means there exists a road between  x  and  y , the distance is  z km   (1z1000)
 

Output
For 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

题目大意:


给出N个点的一棵树,已知两点间距离,也知道每个点的点权值,点权值代表在这个点买或者卖物品花费的或者是得到的钱数、

我们现在只能在一个点购买物品,在一个点卖出物品,问最大的利润。


思路:


我们建立一个源点,使得源点连入各个点,花费是-val【i】,表示我们想要从这个点作为出发点的花费,然后再建立一个汇点,让各个点连入这个点,花费是val【i】,表示我们想要从这个点作为终点的利润。


中间边都要建成负权值,跑一条最长路即可。


Ac代码:

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define ll long long int
struct node
{
    ll from,to,next,w;
}e[100005*7];
ll cont,ss,tt;
ll a[150000];
ll head[150000];
ll vis[150000];
ll dist[150000];
void add(ll from,ll to,ll w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void SPFA()
{
    queue<ll>s;
    memset(vis,0,sizeof(vis));
    for(ll i=0;i<=tt;i++)dist[i]=-10000000000000000;
    dist[0]=0;
    s.push(0);
    while(!s.empty())
    {
        ll u=s.front();
        vis[u]=0;
        s.pop();
        for(ll i=head[u];i!=-1;i=e[i].next)
        {
            ll v=e[i].to;
            ll w=e[i].w;
            if(dist[v]<dist[u]+w)
            {
                dist[v]=dist[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    printf("%lld\n",dist[tt]);
}
int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        cont=0;
        memset(head,-1,sizeof(head));
        ll n;scanf("%lld",&n);
        for(ll i=1;i<=n;i++)scanf("%lld",&a[i]);
        for(ll i=1;i<=n-1;i++)
        {
            ll x,y,w;
            scanf("%lld%lld%lld",&x,&y,&w);
            add(x,y,-w);add(y,x,-w);
        }
        ss=0,tt=n+1;
        for(ll i=1;i<=n;i++)add(ss,i,-a[i]);
        for(ll i=1;i<=n;i++)add(i,tt,a[i]);
        SPFA();
    }
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值