POJ1459 多源多匯網路流經典模型

來源:互聯網
上載者:User

這題是演算法導論上的經典模型,將發電站看成源,使用者看成匯,這樣就形成了多源多匯網路流模型,直接加一個總的源匯即可。

源到發電站的容量為發電站的容量,使用者到匯 的容量為使用者容量,直接跑最大流即可。

 

My Code:

#include <cstdio><br />#include <cstdlib><br />#include <cstring><br />#include <algorithm><br />using namespace std;<br />const int MAX=120;<br />const int oo=0x3f3f3f3f;<br />struct Edge{<br /> int num,ne,cap;<br />}e[2*MAX*MAX];<br />int pre[MAX],pree[MAX],low[MAX],gap[MAX],cur[MAX],p[MAX];<br />int dist[MAX];<br />int n,st,ed,K;<br />int get(){<br /> int ret=0;<br /> bool flag=false;<br /> char ch;<br /> 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 flag?-ret: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 sap(){<br /> int ret=0;<br /> bool done;<br /> memset(dist,0,sizeof(dist));<br /> memset(gap,0,sizeof(gap));<br /> memset(low,0,sizeof(low));<br /> for(int i=0;i<n;i++)cur[i]=p[i];<br /> low[st]=oo;<br /> int u=st;<br /> while(dist[u]<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 /> cur[u]=p[u];<br /> for(int i=p[u];~i;i=e[i].ne){<br /> if(e[i].cap)dist[u]=min(dist[u],dist[e[i].num]+1);<br /> }<br /> gap[dist[u]]++;<br /> if(u!=st)u=pre[u];<br /> }<br /> }<br /> return ret;<br />}<br />int main(){<br /> int nn,np,nc,m;<br /> char ch;<br /> while(~scanf("%d%d%d%d",&nn,&np,&nc,&m)){<br /> n=nn+2;<br /> st=nn;<br /> ed=nn+1;<br /> for(int i=0;i<n;i++)p[i]=-1;K=0;<br /> while(m--){<br /> while((ch=getchar())!='(');<br /> int u=get();<br /> int v=get();<br /> int w=get();<br /> add(u,v,w);<br /> }<br /> while(np--){<br /> int u=get();<br /> int w=get();<br /> add(st,u,w);<br /> }<br /> while(nc--){<br /> int u=get();<br /> int w=get();<br /> add(u,ed,w);<br /> }</p><p> 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.