hdu 3996(最大流最小割的应用)

Gold Mine

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 103    Accepted Submission(s): 27


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
 

Source
 

Recommend
lcy
 


题目:http://acm.hdu.edu.cn/showproblem.php?pid=3996

分析:这题与这题一样??http://blog.csdn.net/fp_hzq/article/details/6735770

又赚到一题,这次多校真是赚到的~~~囧

代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int mm=2222222;
const int mn=11111;
const __int64 oo=100000000000000L;
int node,src,dest,edge;
int ver[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],a[mn],f[mn];
int gold[111],p[111][33],w[111][33],num[111][33][55],ord[111][33][55];
__int64 value[111][33],flow[mm];
inline __int64 min(__int64 a,__int64 b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,__int64 c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
__int64 Dinic_dfs(int u,__int64 exp)
{
    if(u==dest)return exp;
    __int64 tmp;
    for(int &i=work[u],v;i>=0;i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
__int64 Dinic_flow()
{
    int i;
    __int64 ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int main()
{
    int i,j,k,n,m,t,cas=0;
    __int64 sum,cost;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&m);
        for(n=0,k=1;k<=m;++k)
        {
            scanf("%d",&gold[k]);
            for(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(j=1;j<=w[k][i];++j)scanf("%d%d",&num[k][i][j],&ord[k][i][j]);
            }
        }
        prepare(n+2,0,n+1);
        for(k=1,sum=0;k<=m;++k)
            for(i=1;i<=gold[k];++i)
            {
                if(value[k][i]>0)addedge(src,p[k][i],value[k][i]),sum=sum+value[k][i];
                if(value[k][i]<0)addedge(p[k][i],dest,-value[k][i]);
                for(j=1;j<=w[k][i];++j)addedge(p[k][i],p[num[k][i][j]][ord[k][i][j]],oo);
            }
        printf("Case #%d: %I64d\n",++cas,sum-Dinic_flow());
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值