POJ3081 網路流

來源:互聯網
上載者:User

常見的多限制匹配問題,每頭牛需要選擇一種食物和一種飲料,每種食物和飲料只能被一頭牛選到,所以直接建圖,分三部分,左邊為所有的食物,和源連邊,容量為1,右邊是飲料,和匯連邊,容量為1,為了達到每頭牛都只能選擇一種食物和飲料的目的,中間為牛,每頭牛拆成兩個點,兩個點連邊,容量為1,點1和食物連,點2和飲料連,在這基礎上直接跑最大流即可。

 

My Code:

#include <cstdio><br />#include <cstdlib><br />#include <cstring><br />#include <algorithm><br />using namespace std;<br />const int MAX=402;<br />const int oo=0x3f3f3f3f;<br />struct Edge{<br /> int num,ne,cap;<br />}e[MAX*MAX];<br />int pre[MAX],pree[MAX],low[MAX],p[MAX];<br />int gap[MAX],cur[MAX],dist[MAX];<br />int n,st,ed,K;<br />int get(){<br /> char ch;<br /> bool flag=false;<br /> int ret=0;</p><p> while((ch=getchar())<'0'||ch>'9')if(ch=='-')break;<br /> if(ch=='-')flag=true;else ret=ch-'0';<br /> while((ch=getchar())>='0'&&ch<='9')ret=ret*10+ch-'0';<br /> return ret;<br />}<br />int sap(){<br /> int ret=0;<br /> bool done;<br /> memset(gap,0,sizeof(gap));<br /> memset(dist,0,sizeof(dist));<br /> memset(low,0,sizeof(low));<br /> for(int i=0;i<n;i++){<br /> cur[i]=p[i];<br /> }<br /> gap[0]=n;<br /> low[st]=oo;<br /> int u=st;<br /> while(dist[st]<n){<br /> done=true;<br /> for(int i=cur[u];~i;i=e[i].ne){<br /> int v=e[i].num;<br /> cur[u]=i;<br /> if(e[i].cap&&dist[u]==dist[v]+1){<br /> pre[v]=u;pree[v]=i;<br /> low[v]=min(low[u],e[i].cap);<br /> u=v;<br /> if(u==ed){<br /> do{<br /> e[pree[u]].cap-=low[ed];<br /> e[pree[u]^1].cap+=low[ed];<br /> u=pre[u];<br /> }while(u!=st);<br /> ret+=low[ed];<br /> }<br /> done=false;<br /> break;<br /> }<br /> }<br /> if(done){<br /> gap[dist[u]]--;<br /> if(!gap[dist[u]])return ret;<br /> dist[u]=n;<br /> for(int i=p[u];~i;i=e[i].ne){<br /> if(e[i].cap){<br /> dist[u]=min(dist[u],dist[e[i].num]+1);<br /> }<br /> }<br /> gap[dist[u]]++;<br /> cur[u]=p[u];<br /> if(u!=st)u=pre[u];<br /> }<br /> }<br /> return ret;<br />}<br />void add(const int& u,const int& v,const int& cap){<br /> e[K].num=v;e[K].cap=cap;e[K].ne=p[u];p[u]=K++;<br /> e[K].num=u;e[K].cap=0;e[K].ne=p[v];p[v]=K++;<br />}<br />int main(){<br /> int np,f,d;<br /> int m1,m2,x;<br /> while(~scanf("%d%d%d",&np,&f,&d)){<br /> n=2*np+f+d+2;<br /> st=0;<br /> ed=n-1;<br /> for(int i=0;i<n;i++)p[i]=-1;K=0;<br /> for(int i=1;i<=f;i++){<br /> add(st,i,1);<br /> }<br /> for(int i=1;i<=d;i++){<br /> add(i+2*np+f,ed,1);<br /> }<br /> for(int i=1;i<=np;i++){<br /> add(i+f,i+np+f,1);<br /> }<br /> for(int i=1;i<=np;i++){<br /> m1=get();<br /> m2=get();<br /> while(m1--){<br /> x=get();<br /> add(x,i+f,1);<br /> }<br /> while(m2--){<br /> x=get();<br /> add(i+f+np,x+f+2*np,1);<br /> }<br /> }<br /> printf("%d/n",sap());<br /> }<br /> return 0;<br />}<br />

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.