HDU3996 Gold Mine最大权闭合图 2011 Multi-University Training Contest 16 - Host by TJU

 

Gold Mine

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


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
 
和HDU3917Road constructions几乎一样, http://blog.csdn.net/power721/article/details/6665750
只是收益和支出合并成了一个点
 
建图:
s到金矿ij连边,边权为支出
金矿ij到t连边,边权为收益
要采金矿j必须先采金矿i,则i到j连边,边权为inf
 
代码:
#include<cstdio>   
#include<cstring>   
#include<vector>   
#define N 5005   
#define M 1000005   
#define inf 1ll<<60  
#define min(a,b) ((a)<(b)?(a):(b))   
using namespace std;  
  
__int64 n,m,s,t,num,adj[N],dis[N],q[N];  
struct edge  
{  
    __int64 v,w,pre;  
}e[M];  
void insert(__int64 u,__int64 v,__int64 w)  
{  
    e[num]=(edge){v,w,adj[u]};  
    adj[u]=num++;  
    e[num]=(edge){u,0,adj[v]};  
    adj[v]=num++;  
}  
__int64 bfs()  
{  
    __int64 i,x,v,head=0,tail=0;  
    memset(dis,0,sizeof(dis));  
    dis[s]=1;  
    q[++tail]=s;  
    while(head!=tail)  
    {  
        x=q[head=(head+1)%N];  
        for(i=adj[x];~i;i=e[i].pre)  
            if(e[i].w&&!dis[v=e[i].v])  
            {  
                dis[v]=dis[x]+1;  
                if(v==t)  
                    return 1;  
                q[tail=(tail+1)%N]=v;  
            }  
    }  
    return 0;  
}  
__int64 dfs(__int64 x,__int64 limit)  
{  
    if(x==t)  
        return limit;  
    __int64 i,v,tmp,cost=0;  
    for(i=adj[x];~i&&cost<limit;i=e[i].pre)  
        if(e[i].w&&dis[x]==dis[v=e[i].v]-1)  
        {  
            tmp=dfs(v,min(limit-cost,e[i].w));  
            if(tmp)  
            {  
                e[i].w-=tmp;  
                e[i^1].w+=tmp;  
                cost+=tmp;  
            }  
            else  
                dis[v]=-1;  
        }  
    return cost;  
}  
__int64 Dinic()  
{  
    __int64 ans=0;  
    while(bfs())  
        ans+=dfs(s,inf);  
    return  ans;  
}  
int main()  
{  
	__int64 c,cc=0;
	scanf("%I64d",&c);
    while(c--)  
    {  
        __int64 i,j,k,u,v,w1,w2,a,b,idx=2,sum=0,h[105][35]={0};  
        num=0;  
        memset(adj,-1,sizeof(adj));  
        s=0;  
        t=1; 
		scanf("%I64d",&n);
        for(i=0;i<n;i++)
		{
			scanf("%I64d",&m);
			for(j=0;j<m;j++)
			{
				scanf("%I64d%I64d%I64d",&w1,&w2,&k);
				sum+=w2;
				u=h[i][j]=idx++;
				insert(s,u,w1);
				insert(u,t,w2);
				while(k--)
				{
					scanf("%I64d%I64d",&a,&b);
					v=h[a-1][b-1];
					insert(v,u,inf);
				}
			}
		}
        printf("Case #%I64d: %I64d\n",++cc,sum-Dinic());  
    }  
}  

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值