POJ 3281 Dining

Source: Internet
Author: User

It's been inscribed for a day, and now it's over ...

At first, I thought of the maximum match of two, each combination of the edge, calculated the most. It is wrong to think carefully, because there is no consideration for a cow to choose only one group.

Then I thought about the first build and ran the biggest flow, starting with the model of the source--------------food in--> Food out--> drink --and meeting point. The result has been WA, looked discuss, found is wrong.

The reason for the mistake is: if the food is broken down, according to the food-in-> food-out-> drink, such as the plan, although the flow limit is guaranteed, each cow can only choose one food and drink. But the number of cows lost on the side of the food-out-> drink is not a combination of cows, and it is not known which cows are fond of certain sides. This leads to the fact that a cow does not like a match, but because other cows like the pairing, the cow is "forcibly" chosen for this pairing. Naturally, it's WA.

Then you can only use the pattern of the source-----food---ox in--> out--> Drink--meeting point .

Disgusting is, Rujia petition of Dinic incredibly tle ... Then I found a non-recursive dinic AC.

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespacestd;Const intmaxn= +;Const intinf=1<< -;//******************************************//structnode{intV,next; intVal;} EDGE[MAXN*maxn*2];intLEVEL[MAXN];//Hierarchy of verticesintHEAD[MAXN];intque[maxn*Ten];//The vertex used in the BFS for traversal, the record edge in the DFS seek augmentationint  out[Ten*MAXN];//DFS for almost-fixed-point branchesintIND;voidinit () {IND=0; memset (Head,-1,sizeof(head));}voidAddedge (intXintYintz) {EDGE[IND].V=y; Edge[ind].val=Z; Edge[ind].next=Head[x]; HEAD[X]=ind++; EDGE[IND].V=x; Edge[ind].val=0; Edge[ind].next=Head[y]; Head[y]=ind++;}intDinic (intNintSourceintsink) {    intret=0; intH=0, r=0;  while(1)//DFS    {        inti;  for(i=0; i<=n;++i) level[i]=0; H=0, r=0; Level[source]=1; que[0]=source;  while(H&LT;=R)//BFS        {            intt=que[h++];  for(i=head[t];i!=-1; i=Edge[i].next) {                    if(edge[i].val&&level[edge[i].v]==0) {LEVEL[EDGE[I].V]=level[t]+1; que[++r]=edge[i].v; }                }        }        if(level[sink]==0) Break;//meeting Point not found         for(i=0; i<=n;++i) out[i]=Head[i]; intq=-1;  while(1)        {            if(q<0)            {                intCur= out[source];  for(; cur!=-1; cur=Edge[cur].next) {                    if(edge[cur].val&& out[edge[cur].v]!=-1&&level[edge[cur].v]==2)                    {                         Break; }                }                if(cur>=0) {que[++q]=cur;  out[source]=Edge[cur].next; }                Else                     Break; }            intu=edge[que[q]].v; if(U==sink)//an augmented road            {                intDd=inf; intindex=-1;  for(i=0; i<=q;i++)                {                    if(dd>edge[que[i]].val) {DD=Edge[que[i]].val; Index=i; }} RET+=DD;  for(i=0; i<=q;i++) {Edge[que[i]].val-=DD; Edge[que[i]^1].val+=DD; }                 for(i=0; i<=q;i++)                {                    if(edge[que[i]].val==0) {Q=index-1;  Break; }                }            }            Else            {                intCur= out[u];  for(; cur!=-1; cur=Edge[cur].next) {                    if(edge[cur].val&& out[edge[cur].v]!=-1&&level[u]+1==LEVEL[EDGE[CUR].V]) {                         Break; }                }                if(cur!=-1) {que[++q]=cur;  out[u]=Edge[cur].next; }                Else                {                     out[u]=-1; Q--; }            }        }    }    returnret;}intN,f,d,ff,dd;intFF[MAXN],DD[MAXN];//Input/OutputintMain () { while(~SCANF ("%d%d%d", &n, &f, &D) {init (); //s=0; //t=2*n+f+d+1;         for(intI=2*n+1; i<=2*n+f;i++) Addedge (0I1);  for(intI=2*n+1+f;i<=2*n+f+d;i++) Addedge (i,2*n+f+d+1,1);  for(intI=1; i<=n;i++) Addedge (i,i+n,1);  for(intI=1; i<=n;i++) {scanf ("%d%d",&ff,&DD);  for(intj=1; j<=ff;j++) scanf ("%d",&Ff[j]);  for(intj=1; j<=dd;j++) scanf ("%d",&Dd[j]);  for(intj=1; j<=ff;j++)                 for(intk=1; k<=dd;k++) {Addedge (2*n+ff[j],i,1); Addedge (i+n,2*n+f+dd[k],1); }} printf ("%d\n", Dinic (2*n+f+d+Ten,0,2*n+f+d+1)); }    return 0;}

POJ 3281 Dining

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.