hdu 3996 Gold Mine 最大权闭合图

Problem Description
Long long ago, there is a gold mine.The mine consist of many layout, so some area is easy to dig, but some is very hard to dig.To dig one gold, we should cost some value and then gain some value. There are many area that have gold, because of the layout, if one people want to dig one gold in some layout, he must dig some gold on some layout that above this gold's layout. A gold seeker come here to dig gold.The question is how much value the gold he can dig, suppose he have infinite money in the begin.
 


 

Input
First line the case number.(<=10)

Then for every case:
  one line for layout number.(<=100)
  for every layout
  first line gold number(<=25)
  then one line for the dig cost and the gold value(32bit integer), the related gold number that must be digged first(<=50)

then w lines descripte the related gold followed, each line two number, one layout num, one for the order in that layout
see sample for details
 


 

Output
Case #x: y.
x for case number, count from 1.
y for the answer.
 


 

Sample Input
  
  
1 2 1 10 100 0 2 10 100 1 1 1 10 100 1 1 1
 


 

Sample Output
  
  
Case #1: 270

 

 //

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const __int64 N=2010;
const __int64 M=550000;
const __int64 inf=(1LL<<58);
__int64 head[N];
struct Edge
{
    __int64 u,v,next,w;
} edge[M];
__int64 cnt,n,s,t;//n从0开始  0->n-1
void addedge(__int64 u,__int64 v,__int64 w)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
__int64 sap()
{
    __int64 pre[N],cur[N],dis[N],gap[N];
    __int64 flow=0,aug=inf,u;
    bool flag;
    for(__int64 i=0; i<n; i++)
    {
        cur[i]=head[i];
        gap[i]=dis[i]=0;
    }
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        flag=0;
        for(__int64 &j=cur[u]; j!=-1; j=edge[j].next)
        {
            __int64 v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(u==t)
                {
                    flow+=aug;
                    while(u!=s)
                    {
                        u=pre[u];
                        edge[cur[u]].w-=aug;
                        edge[cur[u]^1].w+=aug;
                    }
                    aug=inf;
                }
                break;
            }
        }
        if(flag) continue;
        __int64 mindis=n;
        for(__int64 j=head[u]; j!=-1; j=edge[j].next)
        {
            __int64 v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)
            break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}

//初始化  cnt=0;memset(head,-1,sizeof(head));


//最大权闭合图 = sigma(val)(val>0)-最小割
//建图:构造一个源点S,汇点T。我们将S与所有权值为正的点连一条容
//量为其权值的边,将所有权值为负的点与T连一条容量为其权值的绝对值
//的边,原来的边将其容量定为正无穷。
//最小割所产生的两个集合中,其源点S所在集合(除去S)为最大权闭合图。

__int64 gold[111],p[111][33],w[111][33],num[111][33][55],ord[111][33][55];
__int64 value[111][33];

int main()
{
    int ci,cas=0;
    __int64 sum,cost;
    scanf("%d",&ci);
    while(ci--)
    {
        __int64 m;
        scanf("%I64d",&m);
        n=0;
        for(int k=1;k<=m;++k)
        {
            scanf("%I64d",&gold[k]);
            for(int i=1;i<=gold[k];++i)
            {
                scanf("%I64d%I64d%d",&cost,&value[k][i],&w[k][i]);
                p[k][i]=++n;
                value[k][i]-=cost;
                for(int j=1;j<=w[k][i];++j)scanf("%I64d%I64d",&num[k][i][j],&ord[k][i][j]);
            }
        }
        n+=2;
        s=0,t=n-1;
        cnt=0;
        memset(head,-1,sizeof(head));
        sum=0;
        for(int k=1;k<=m;++k)
            for(int i=1;i<=gold[k];++i)
            {
                if(value[k][i]>0)addedge(s,p[k][i],value[k][i]),sum=sum+value[k][i];
                if(value[k][i]<0)addedge(p[k][i],t,-value[k][i]);
                for(int j=1;j<=w[k][i];++j)addedge(p[k][i],p[num[k][i][j]][ord[k][i][j]],inf);
            }
        printf("Case #%d: %I64d\n",++cas,sum-sap());
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值