HDU 3996 Gold Mine

Gold Mine

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


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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>

#define LL_INF 0x7fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 60005
#define E 5000010

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;

const LL INT_INF=((LL)1)<<62;

struct Edge
{
    int en,next;
    LL cap,flow;
} edge[E];
int head[N] , tot , now[N];
int source,sink,tot_num;
int pre[N] , dis[N] , gap[N];

void add_edge(int st,int en,LL cap)
{
    edge[tot].en=en;
    edge[tot].cap=cap;
    edge[tot].flow=0;
    edge[tot].next=head[st];
    head[st]=tot++;

    edge[tot].en=st;
    edge[tot].cap=0;
    edge[tot].flow=0;
    edge[tot].next=head[en];
    head[en]=tot++;
}

void augment(LL flow)
{
    for(int i=source;i!=sink;i=edge[now[i]].en)
    {
        edge[now[i]].flow+=flow;
        edge[now[i]^1].flow-=flow;
    }
}

LL sap()
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(int i=0;i<tot_num;i++)
        now[i]=head[i];
    gap[0]=tot_num;
    int point=source;
    LL flow=0,min_flow=INT_INF;
    while(dis[source]<tot_num)
    {
        bool fg=false;
        for(int i=now[point];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow>0 && dis[point]==dis[edge[i].en]+1)
            {
                min_flow=min(min_flow,edge[i].cap-edge[i].flow);
                now[point]=i;
                pre[edge[i].en]=point;
                point=edge[i].en;
                if(point==sink)
                {
                    flow+=min_flow;
                    augment(min_flow);
                    point=source;
                    min_flow=INT_INF;
                }
                fg=true;
                break;
            }
        if(fg) continue;
        if(--gap[dis[point]]==0) break;
        int Min=tot_num;
        for(int i=head[point];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow>0 && Min>dis[edge[i].en])
            {
                Min=dis[edge[i].en];
                now[point]=i;
            }
        gap[dis[point]=Min+1]++;
        if(point!=source) point=pre[point];
    }
    return flow;
}

LL build(int n)
{
    LL ans=0;
    memset(head,-1,sizeof(head));
    tot=0;
    source=0; sink=N-2; tot_num=N;
    for(int i=1,num;i<=n;i++)
    {
        scanf("%d",&num);
        for(int j=1,r,a,b; j<=num; j++)
        {
            LL c,v;
            scanf("%I64d%I64d%d",&c,&v,&r);
            add_edge(source,i*250+j,v);
            add_edge(i*250+j,sink,c);
            ans+=v;
            while(r--)
            {
                scanf("%d%d",&a,&b);
                add_edge(i*250+j,a*250+b,INT_INF);
            }
        }
    }
    return ans;
}

int main()
{
    int t; scanf("%d",&t);
    for(int ca=1; ca<=t; ca++)
    {
        int n; scanf("%d",&n);
        LL ans=build(n);
        ans-=sap();
        printf("Case #%d: %I64d\n",ca,ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章