【HDU 6201】transaction transaction transaction 【费用流】

来源:互联网 发布:淘宝基础版全套代码 编辑:程序博客网 时间:2024/06/15 04:32

transaction transaction transaction

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

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 n−1 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 (1≤T≤10) , the number of test cases.
For each test case:
first line contains an integer n (2≤n≤100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1≤Price≤10000)
then follows n−1 lines, each contains three numbers x, y and z which means there exists a road between x and y, the distance is zkm (1≤z≤1000).

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

Source
2017 ACM/ICPC Asia Regional Shenyang Online

题意 n个点,n-1个边,对于同一种物品,每一个点都有一个价格,我们现在要求只能够在一个点买,一个点卖,然后边上还有花费,问最后能够得到的最大钱是多少。

代码

#include<bits/stdc++.h>using namespace std ;typedef long long LL ;const int MAXN = 100000+10 ;const int MAXM = 1e5 ;const int mod  = 1e9+7 ;const int  inf = 0x3f3f3f3f; struct Edge {    int  to,cap,flow,cost,next;}edge[MAXN*10];int  n,m;int head[MAXN],top;void init(){    memset(head,-1,sizeof(head));    top=0;}void addedge(int a,int  b,int w,int  c){    edge[top].to=b;edge[top].cap=w;edge[top].flow=0;edge[top].cost=c;edge[top].next=head[a];    head[a]=top++;    edge[top].to=a;edge[top].cap=0;edge[top].flow=0;edge[top].cost=-c;edge[top].next=head[b];    head[b]=top++;}int S,T;void getmap(){  // 重点     S=0;T=n+2; int  cost; // 流量都为1     addedge(n+1,T,1,0);//为了限流,最后我们只要一条路。    for(int i=1;i<=n;i++) {          scanf("%d",&cost);         addedge(S,i,1,+cost);//源点和所有的顶点都连边,         addedge(i,n+1,1,-cost);//所有顶点和前汇点连边    }    int a,b,c,d;    while(m--){        scanf("%d%d%d",&a,&b,&c);        addedge(a,b,1,c);        addedge(b,a,1,c);    }}int  pre[MAXN],dis[MAXN];bool vis[MAXN];bool  spfa(int s,int t){    queue<int>Q;    memset(dis,inf,sizeof(dis));    memset(vis,false,sizeof(vis));    memset(pre,-1,sizeof(pre));    dis[s]=0;vis[s]=true;Q.push(s);    while(!Q.empty()){        int now=Q.front();Q.pop();vis[now]=0;        for(int i=head[now];i!=-1;i=edge[i].next){            Edge  e=edge[i];            if(e.cap>e.flow&&dis[e.to]>dis[now]+e.cost){                dis[e.to]=dis[now]+e.cost;                pre[e.to]=i;                if(!vis[e.to]){                    vis[e.to]=true;                    Q.push(e.to);                }            }        }    }    return pre[t]!=-1;}void  MCMF(int  s,int t,int &flow,int  &cost){    flow=cost=0;    while(spfa(s,t)){        int  Min=inf;        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){            if(Min>edge[i].cap-edge[i].flow){                Min=edge[i].cap-edge[i].flow;            }        }        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){              edge[i].flow+=Min;              edge[i^1].flow-=Min;              cost+=edge[i].cost*Min;        }        flow+=Min;    }}int main(){    int  ncase;scanf("%d",&ncase);    while(ncase--){        scanf("%d",&n);m=n-1;        init();        getmap();        int flow,ans;        MCMF(S,T,flow,ans);// 因为我们建的图,不会有断路的情况,所以这里不用特判         printf("%d\n",-ans);    }    return  0;}
原创粉丝点击