HDU 3996 Gold Mine (min cut-max weight closed)

Source: Internet
Author: User

Address: HDU 3996

Very simple, the biggest right closed question, but there is a small pitfall .. That is, int64 .... But why do I hand in the returned tle ....

The idea of creating a map: numbers the gold mines in each layer. Easy to handle. Then, the Source Vertex connects to the positive weight point, and the sink vertex connects to the negative weight point. Each vertex connects to the dependent vertex.

If you subtract the largest stream from the sum of the right, the answer is displayed.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const LL INF=1e15;int head[3000], cnt, source, sink, nv, ren[120];LL val[3000];int d[3000], num[3000], pre[3000], cur[3000];struct node{    int u, v, next;    LL cap;} edge[1000000];void add(int u, int v, LL cap){    edge[cnt].v=v;    edge[cnt].cap=cap;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].cap=0;    edge[cnt].next=head[v];    head[v]=cnt++;}void bfs(){    memset(d,-1,sizeof(d));    memset(num,0,sizeof(num));    queue<int>q;    q.push(sink);    d[sink]=0;    num[0]=1;    while(!q.empty())    {        int u=q.front();        q.pop();        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            if(d[v]==-1)            {                d[v]=d[u]+1;                num[d[v]]++;                q.push(v);            }        }    }}LL isap(){    memcpy(cur,head,sizeof(cur));    int u=pre[source]=source, i;    LL flow=0;    bfs();    while(d[source]<nv)    {        if(u==sink)        {            LL f=INF;            int pos;            for(i=source; i!=sink; i=edge[cur[i]].v)            {                if(f>edge[cur[i]].cap)                {                    f=edge[cur[i]].cap;                    pos=i;                }            }            for(i=source; i!=sink; i=edge[cur[i]].v)            {                edge[cur[i]].cap-=f;                edge[cur[i]^1].cap+=f;            }            flow+=f;            u=pos;        }        for(i=cur[u]; i!=-1; i=edge[i].next)        {            if(d[edge[i].v]+1==d[u]&&edge[i].cap)            {                break;            }        }        if(i!=-1)        {            cur[u]=i;            pre[edge[i].v]=u;            u=edge[i].v;        }        else        {            if(--num[d[u]]==0) break;            int mind=nv;            for(i=head[u]; i!=-1; i=edge[i].next)            {                if(mind>d[edge[i].v]&&edge[i].cap)                {                    mind=d[edge[i].v];                    cur[u]=i;                }            }            d[u]=mind+1;            num[d[u]]++;            u=pre[u];        }    }    return flow;}int main(){    int t, n, m, i, num=0, k, tot, j, c, d;    LL a, b, sum;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        num++;        memset(head,-1,sizeof(head));        cnt=0;        sum=tot=0;        for(j=1; j<=n; j++)        {            scanf("%d",&m);            ren[j]=tot;            for(i=1; i<=m; i++)            {                scanf("%I64d%I64d%d",&a,&b,&k);                val[++tot]=b-a;                while(k--)                {                    scanf("%d%d",&c,&d);                    add(tot,ren[c]+d,INF);                }            }        }        source=0;        sink=tot+1;        nv=sink+1;        for(i=1; i<=tot; i++)        {            if(val[i]>0)            {                add(source,i,val[i]);                sum+=val[i];            }            else                add(i,sink,-val[i]);        }        printf("Case #%d: ",num);        printf("%I64d\n",sum-isap());    }    return 0;}


HDU 3996 Gold Mine (min cut-max weight closed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.