Hdu 6201 transaction transaction transaction【最长路】

来源:互联网 发布:win8安装不了软件 编辑:程序博客网 时间:2024/06/15 22:15

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 it city. Kelukin will take taxi, whose price is 1yuan 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 ith number means the prices in ith city; (1Price10000) 
then follows n1 lines, each contains three numbers xy and z which means there exists a road between x and y, the distance is zkm (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 301 3 23 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 intstruct 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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩好动精力不集中怎么办 孩子好动精力不集中怎么办 一岁半宝宝咳嗽半个月了怎么办 6岁儿童上课调皮怎么办 小孩挨揍后精神失控怎么办 孩子不听话把我胃气疼了怎么办 因为孩子不听话夫妻经常吵架怎么办 11岁儿子不听话了怎么办 二十岁的儿子还不听话怎么办 幼儿园小班幼儿不听老师的话怎么办 幼师对待不听话的孩子该怎么办 2岁宝宝不吃饭只喝奶怎么办 孩子哭着喊妈妈不睡觉怎么办 孩子晚上不睡觉一直哭怎么办 孩子不睡觉还哭怎么办 孩子晚上不睡觉老哭怎么办? 4岁宝宝叛逆期怎么办 驾考紧张脚抖怎么办 驾考科目三紧张怎么办 孩子不自信容易紧张怎么办 在人多时候紧张怎么办 考科目二很紧张怎么办 明天出成绩很紧张怎么办 一紧张就射精了怎么办 孩子在幼儿园表现不好怎么办 小孩子在幼儿园表现的不好怎么办 学籍档案写错了怎么办 发展报告填错了怎么办 学生发展报告丢了怎么办 大一新生入学团员档案袋丢失怎么办 三年级孩子语文成绩差怎么办 运气不好时 我们该怎么办 猫走了财运变差怎么办 特别在意别人的眼光怎么办 入职两个月没有业绩怎么办 在学舞蹈中孩子怕疼怎么办 我生二胎怕疼怎么办 孩子的执行力差怎么办 怀孕三个月不想要孩子怎么办 生完孩子老是掉头发怎么办 5岁半宝宝有狐臭怎么办?