poj 3308 (最大流)

來源:互聯網
上載者:User

#include<stdio.h>#include<math.h>#include<string.h>const int N=3000;#define inf 100.0int head[N],num,ans,start,end,gap[N],dis[N];struct edge{int st,ed,next;double flow;}e[N*100];void addedge(int x,int y,double w){e[num].st=x;e[num].ed=y;e[num].flow=w;e[num].next=head[x];head[x]=num++;e[num].st=y;e[num].ed=x;e[num].flow=0;e[num].next=head[y];head[y]=num++;}double dfs(int u,double minflow){if(u==end)return minflow;int i,v;double f,flow=0.0;for(i=head[u];i!=-1;i=e[i].next){v=e[i].ed;if(e[i].flow>0){if(dis[v]+1==dis[u]){f=dfs(v,e[i].flow>minflow-flow?minflow-flow:e[i].flow);flow+=f;e[i].flow-=f;e[i^1].flow+=f;if(minflow-flow<=1e-8)return flow;  if(dis[start]>=ans)return flow;  }}}if(--gap[dis[u]]==0)dis[start]=ans;dis[u]++;gap[dis[u]]++;return flow;}double isap(){double maxflow=0.0;memset(gap,0,sizeof(gap));memset(dis,0,sizeof(dis));gap[0]=ans;while(dis[start]<ans)maxflow+=dfs(start,inf);return maxflow;}int main(){int i,n,m,k,x,y,t;double w;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&k);memset(head,-1,sizeof(head));num=0;start=0;end=n+m+1;ans=end+1;for(i=1;i<=n;i++){scanf("%lf",&w);addedge(start,i,log(w));}for(i=1;i<=m;i++){scanf("%lf",&w);addedge(i+n,end,log(w));}while(k--){scanf("%d%d",&x,&y);addedge(x,y+n,inf);}w=isap();printf("%.4lf\n",exp(w));}return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.