BZOJ 1834 ZJOI2010 network 網路擴容 Dinic+EK費用流

來源:互聯網
上載者:User

標籤:bzoj   bzoj1834   dinic   ek費用流   

題目大意:給定一個n個點m條邊的無向圖,每條邊有一個擴容費用c,代表每擴容1流量的花費,求最大流及將最大流擴大k的最小費用

第一問直接跑最大流

第二問將每條邊的起始點向終點串連一條流量為正無窮、費用為c的邊 然後將n向匯點連一條流量為ans+k 費用為0的邊 跑最小費用最大流即可

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 5010#define INF 0x3f3f3f3f#define S 1#define T (n+1)using namespace std;struct edge{int x,y,f,c;}edges[M];struct abcd{int to,f,c,next;}table[M<<2];int head[M],tot=1;int n,m,k,ans,anscost;int dpt[M];void Add(int x,int y,int f,int c){table[++tot].to=y;table[tot].f=f;table[tot].c=c;table[tot].next=head[x];head[x]=tot;}bool BFS(){static int q[M],r,h;int i;memset(dpt,-1,sizeof dpt);r=h=0;q[++r]=S;dpt[S]=1;while(r!=h){int x=q[++h];for(i=head[x];i;i=table[i].next)if(table[i].f&&!~dpt[table[i].to]){dpt[table[i].to]=dpt[x]+1;q[++r]=table[i].to;if(table[i].to==T)return true;}}return false;}int Dinic(int x,int flow){int i,left=flow;if(x==T) return flow;for(i=head[x];i&&left;i=table[i].next)if(table[i].f&&dpt[table[i].to]==dpt[x]+1){int temp=Dinic(table[i].to,min(left,table[i].f) );if(!temp) dpt[table[i].to]=-1;left-=temp;table[i].f-=temp;table[i^1].f+=temp;}return flow-left;}bool EK(){static int q[65540],flow[M],cost[M],from[M];static bool v[M];static unsigned short r,h;int i;memset(cost,0x3f,sizeof cost);cost[S]=0;flow[S]=INF;q[++r]=S;while(r!=h){int x=q[++h];v[x]=0;for(i=head[x];i;i=table[i].next)if(table[i].f)if(cost[table[i].to]>cost[x]+table[i].c){cost[table[i].to]=cost[x]+table[i].c;flow[table[i].to]=min(flow[x],table[i].f);from[table[i].to]=i;if(!v[table[i].to])v[table[i].to]=1,q[++r]=table[i].to;}}if(cost[T]==INF) return false;anscost+=flow[T]*cost[T];for(i=from[T];i;i=from[table[i^1].to])table[i].f-=flow[T],table[i^1].f+=flow[T];return true;}int main(){int i;cin>>n>>m>>k;for(i=1;i<=m;i++){scanf("%d%d",&edges[i].x,&edges[i].y);scanf("%d%d",&edges[i].f,&edges[i].c);Add(edges[i].x,edges[i].y,edges[i].f,0);Add(edges[i].y,edges[i].x,0,0);}Add(n,T,INF,0);Add(T,n,0,0);while( BFS() )ans+=Dinic(S,INF);table[tot-1].f=k;for(i=1;i<=m;i++){Add(edges[i].x,edges[i].y,INF,edges[i].c);Add(edges[i].y,edges[i].x,0,-edges[i].c);}while( EK() );cout<<ans<<' '<<anscost<<endl;}


BZOJ 1834 ZJOI2010 network 網路擴容 Dinic+EK費用流

聯繫我們

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