The King of Luogu P1402 Hotel (Network stream)

Source: Internet
Author: User

Thinking of solving problems

It should be more obvious that this is a network flow, will be $s$ with the room, the room with the people, the people and the food side, food and meeting point edge, edge of the flow of 1. But this is wrong, because it is possible for a person to run past 2 of the traffic, so the person will be removed to limit the flow.

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<queue>using namespacestd;Const intMAXN =505;Const intMAXM =50005;Const intINF =0x3f3f3f3f; inlineintRd () {intx=0;CharCh=GetChar ();  while(!isdigit (CH)) ch=GetChar ();  while(IsDigit (CH)) {x= (x<<1) + (x<<3) +ch-'0'; ch=GetChar ();} returnx;}intN,p,q,head[maxn],cnt=1,to[maxm<<1],nxt[maxm<<1],val[maxm<<1];intans,s,t= -, D[maxn];queue<int>Q;inlinevoidAddintBgintEdintW) {to[++cnt]=ed,nxt[cnt]=head[bg],val[cnt]=w,head[bg]=CNT;} InlineBOOLBFs () { while(Q.size ()) Q.pop (); memset (d,0,sizeof(d)); d[s]=1;    Q.push (S);  while(Q.size ()) {intx=Q.front ();        Q.pop ();  for(RegisterintI=head[x];i;i=Nxt[i]) {            intu=To[i]; if(!d[u] &&Val[i]) {D[u]=d[x]+1;                Q.push (U); if(u==t)return true; }        }    }    return false;}intDinic (intXintflow) {    if(x==t)returnflow; intres=flow,k;  for(RegisterintI=head[x];i && res;i=Nxt[i]) {        intu=To[i]; if(d[u]==d[x]+1&&Val[i]) {k=dinic (U,min (Val[i],flow)); if(!k) d[u]=0; Val[i]-=k;val[i^1]+=k;res-=K; }    }    returnflow-Res;}intMain () {n=rd (), P=rd (), Q=rd ();intx;  for(intI=1; i<=p;i++) Add (S,i,1), add (I,s,0);  for(intI=1; i<=n;i++)         for(intj=1; j<=p;j++) {x=rd ();if(!x)Continue; Add (J,i+p,1), add (I+p,j,0); }     for(intI=1; i<=n;i++) Add (I+p,i+p+n,1), add (I+p+n,i+p,0);  for(intI=1; i<=n;i++)         for(intj=1; j<=q;j++) {x=rd ();if(!x)Continue; Add (i+p+n,j+n*2+p,1), Add (j+n*2+p,i+p+n,0); }     for(intI=1; i<=q;i++) Add (i+n*2+p,t,1), Add (t,i+n*2+p,0);  while(BFS ()) ans+=dinic (S,inf); cout<<ans; return 0;}
View Code

The King of Luogu P1402 Hotel (Network stream)

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.