hdu 3996 最大权闭合子图

Gold Mine

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


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   |   We have carefully selected several similar problems for you:   4000  3999  3993  1274  2059


题意:有一些金矿区域,挖一个金矿时必须挖掉上边的跟他关联的,为最多赚的钱数。输入解释:第一个行是样例的组数。第二行表示有n个区域,接下来的一行m表示第i个区域的金矿的个数为m。接下来的m行为这个区域金矿花费的钱数,获得钱数,以及相关连的金矿的个数w,(下面的w行就是表示这些相关联的金矿的区域和在这个区域的第几个)。

思路:最大闭合图。类似poj 2987.建图时把每层的金矿数固定,然后编号建边。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/8 22:19:12
File Name :treap2.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 10000000000000LL
#define eps 1e-8
#define pi acos(-1.0)
typedef __int64 ll;
const ll maxn=3199;
const ll maxm=400010;
struct Edge{
    ll to,next,cap,flow;
    Edge(){};
    Edge(ll _next,ll _to,ll _cap,ll _flow)`{
        next=_next;to=_to;cap=_cap;flow=_flow;
    }
}edge[maxm];
ll head[maxn],tol,gap[maxn],dep[maxn],cur[maxn];
void addedge(ll u,ll v,ll flow){
    edge[tol]=Edge(head[u],v,flow,0);head[u]=tol++;
    edge[tol]=Edge(head[v],u,0,0);head[v]=tol++;
}
ll Q[maxn];
void bfs(ll start,ll end){
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]++;ll front=0,rear=0;
    dep[end]=0;Q[rear++]=end;
    while(front!=rear){
        ll u=Q[front++];
        for(ll i=head[u];i!=-1;i=edge[i].next){
            ll v=edge[i].to;if(dep[v]==-1&&edge[i].cap)
                Q[rear++]=v,dep[v]=dep[u]+1,gap[dep[v]]++;
        }
    }
}
ll S[maxn];
ll sap(ll start,ll end,ll N){
    bfs(start,end);
    memcpy(cur,head,sizeof(head));
    ll top=0,u=start,ans=0;
    while(dep[start]<N){
        if(u==end){
            ll MIN=INF,id;
            for(ll i=0;i<top;i++)
                if(MIN>edge[S[i]].cap-edge[S[i]].flow)
                    MIN=edge[S[i]].cap-edge[S[i]].flow,id=i;
            for(ll i=0;i<top;i++)
                edge[S[i]].flow+=MIN,edge[S[i]^1].flow-=MIN;
            ans+=MIN,top=id,u=edge[S[top]^1].to;
            continue;
        }
        bool flag=0;ll v;
        for(ll i=cur[u];i!=-1;i=edge[i].next){
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
                flag=1;cur[u]=i;break;
            }
        }
        if(flag){
            S[top++]=cur[u];u=v;continue;
        }
        ll MIN=N;
        for(ll i=head[u];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<MIN)
                MIN=dep[edge[i].to],cur[u]=i;
        if(--gap[dep[u]]==0)break;gap[dep[u]=MIN+1]++;
        if(u!=start)u=edge[S[--top]^1].to;
    }
    return ans;
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     ll T,t;
	 scanf("%I64d",&T);
	 for(ll t=1;t<=T;t++){
		 memset(head,-1,sizeof(head));tol=0;
		 ll sum=0;
		 ll laynum;
		 scanf("%I64d",&laynum);
		 for(ll i=1;i<=laynum;i++){
			 ll jin;
			 scanf("%I64d",&jin);
			 for(ll j=1;j<=jin;j++){
				 ll a,b,c;
				 scanf("%I64d%I64d%I64d",&a,&b,&c);
				 b-=a;
				 if(b>0){
					 sum+=b;
					 addedge(0,i*26+j,b);
				 }
				 else addedge(i*26+j,3000,-b);
				 while(c--){
					 ll u,v;
					 scanf("%I64d%I64d",&u,&v);
					 addedge(i*26+j,u*26+v,INF);
				 }
			 }
		 }
		 ll cnt=sap(0,3000,10000);
		  printf("Case #%I64d: %I64d\n",t,sum-cnt);  
	 }
     return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值