HDU 3879 最大权闭合图

来源:互联网 发布:福建省网络教育平台 编辑:程序博客网 时间:2024/05/22 10:48

Base Station

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65768/32768 K (Java/Others)
Total Submission(s): 1788    Accepted Submission(s): 758


Problem Description
A famous mobile communication company is planning to build a new set of base stations. According to the previous investigation, n places are chosen as the possible new locations to build those new stations. However, the condition of each position varies much, so the costs to built a station at different places are different. The cost to build a new station at the ith place is Pi (1<=i<=n).

When complete building, two places which both have stations can communicate with each other.

Besides, according to the marketing department, the company has received m requirements. The ith requirement is represented by three integers Ai, Bi and Ci, which means if place Aand Bi can communicate with each other, the company will get Ci profit.

Now, the company wants to maximize the profits, so maybe just part of the possible locations will be chosen to build new stations. The boss wants to know the maximum profits.
 

Input
Multiple test cases (no more than 20), for each test case:
The first line has two integers n (0<n<=5000) and m (0<m<=50000).
The second line has n integers, P1 through Pn, describes the cost of each location.
Next m line, each line contains three integers, Ai, Bi and Ci, describes the ith requirement.
 

Output
One integer each case, the maximum profit of the company.
 

Sample Input
5 51 2 3 4 51 2 32 3 41 3 31 4 24 5 3
 

Sample Output
4
 

Author
liulibo
 

Source
2011 Multi-University Training Contest 5 - Host by BNU
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3657 3338 3277 3887 3889 

分析:最大权闭合图模型。

代码:

//Isap算法,复杂度O(n^2m)#pragma comment(linker,"/STACK:102400000,102400000")#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <vector>#include <string>#include <math.h>#include <queue>#include <stack>#include <map>#include <set>using namespace std;typedef long long ll;   //记得必要的时候改成无符号const int maxn=55005;const int maxm=1000005;const int INF=1000000000;struct EdgeNode{    int from;    int to;    int cost;    int next;}edge[maxm];int head[maxn],cnt;void add(int x,int y,int z){    edge[cnt].from=x;edge[cnt].to=y;edge[cnt].cost=z;edge[cnt].next=head[x];head[x]=cnt++;    edge[cnt].from=y;edge[cnt].to=x;edge[cnt].cost=0;edge[cnt].next=head[y];head[y]=cnt++;}void init(){    cnt=0;    memset(head,-1,sizeof(head));}int S,T,n,m;int d[maxn],gap[maxn],curedge[maxn],pre[maxn];//curedge[]为当前弧数组,pre为前驱数组int sap(int S,int T,int n)  //n为点数{    int cur_flow,flow_ans=0,u,tmp,neck,i;    memset(d,0,sizeof(d));    memset(gap,0,sizeof(gap));    memset(pre,-1,sizeof(pre));    for(i=0;i<=n;i++)curedge[i]=head[i]; //初始化当前弧为第一条邻接表    gap[0]=n;    u=S;    while(d[S]<n)             //当d[S]>=n时,网络中肯定出现了断层    {        if(u==T)        {            cur_flow=INF;            for(i=S;i!=T;i=edge[curedge[i]].to)            {                           //增广成功,寻找瓶颈边                if(cur_flow>edge[curedge[i]].cost)                {                    neck=i;                    cur_flow=edge[curedge[i]].cost;                }            }            for(i=S;i!=T;i=edge[curedge[i]].to)            {                             //修改路径上的边容量                tmp=curedge[i];                edge[tmp].cost-=cur_flow;                edge[tmp^1].cost+=cur_flow;            }            flow_ans+=cur_flow;            u=neck;                     //下次增广从瓶颈边开始        }        for(i=curedge[u];i!=-1;i=edge[i].next)            if(edge[i].cost&&d[u]==d[edge[i].to]+1)               break;        if(i!=-1)        {            curedge[u]=i;            pre[edge[i].to]=u;            u=edge[i].to;        }        else        {            if(0==--gap[d[u]])break;    //gap优化            curedge[u]=head[u];            for(tmp=n,i=head[u];i!=-1;i=edge[i].next)                if(edge[i].cost)                   tmp=min(tmp,d[edge[i].to]);            d[u]=tmp+1;            ++gap[d[u]];            if(u!=S)u=pre[u];           //重标号并且从当前点前驱重新增广        }    }    return flow_ans;}int main(){    int ans,i,x,y,z,sum;    while(~scanf("%d%d",&n,&m))    {        sum=0;        init(); S=0; T=n+m+1; ans=n;        for(i=1;i<=n;i++){            scanf("%d",&x);            add(i,T,x);        }        for(i=1;i<=m;i++){            scanf("%d%d%d",&x,&y,&z);            ans++;            sum+=z;            add(S,ans,z);            add(ans,x,INF);            add(ans,y,INF);        }        n=T+1;        printf("%d\n",sum-sap(S,T,n));    }    return 0;}




0 0
原创粉丝点击