【传送门:BZOJ1834】
简要题意:
给出n个点,m条边的有向图,给出每条边的流量c和费用w(每条边都可以扩增自己的流量,每增加1流量就需要w的花费)
求出从1到n的最大流,并且求出使最大流+k的最小花费
题解:
第一个答案用网络流直接求肯定是没问题
第二个答案就要用费用流来做
首先因为求第一个答案的时候已经将能构成最大流的路径上的每条边的流量都减少了,所以剩下了一个残留图
我们会发现其实残留图上还有一些边的流量没有流尽,那么我们就用每条边的剩余流量建一条流量为当前剩余流量而费用为0的边,并且建一条流量无限,费用为原费用的边,如果这条边已经流尽了,那么我们直接建一条流量无限,费用为原费用的边,而实际操作中是可以把这两个步骤合并的
而其实我们并不需要将所有边都重新建,只需要每条边都加建一条流量无限,费用为原费用的边,而剩余流量所建的边就是残留图上的边
最后我们还要新建一个汇点,使n连向汇点,流量为k,费用为0,这样就可以保证总流量不超过k
参考代码:
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct node{ ???int x,y,c,d,next,other; ???node() ???{ ???????d=0; ???}}a[21000];int len,last[5100];void ins(int x,int y,int c,int d){ ???int k1=++len,k2=++len; ???a[k1].x=x;a[k1].y=y;a[k1].c=c;a[k1].d=d; ???a[k1].next=last[x];last[x]=k1; ???a[k2].x=y;a[k2].y=x;a[k2].c=0;a[k2].d=-d; ???a[k2].next=last[y];last[y]=k2; ???a[k1].other=k2; ???a[k2].other=k1;}int st,ed;int h[5100],list[5100];bool bt_h(){ ???int head=1,tail=2; ???memset(h,0,sizeof(h)); ???h[st]=1; ???list[1]=st; ???while(head!=tail) ???{ ???????int x=list[head]; ???????for(int k=last[x];k;k=a[k].next) ???????{ ???????????int y=a[k].y; ???????????if(h[y]==0&&a[k].c>0) ???????????{ ???????????????h[y]=h[x]+1; ???????????????list[tail++]=y; ???????????} ???????} ???????head++; ???} ???if(h[ed]==0) return false; ???else return true;}int findflow(int x,int f){ ???if(x==ed) return f; ???int s,t=0; ???for(int k=last[x];k;k=a[k].next) ???{ ???????int y=a[k].y; ???????if(h[y]==(h[x]+1)&&a[k].c>0&&f>t) ???????{ ???????????s=findflow(y,min(a[k].c,f-t)); ???????????t+=s; ???????????a[k].c-=s;a[a[k].other].c+=s; ???????} ???} ???if(t==0) h[x]=0; ???return t;}int d[5100];bool v[5100];int ans;int pos[5100],pre[5100];bool spfa(){ ???for(int i=st;i<=ed;i++) d[i]=999999999; ???d[st]=0; ???memset(v,false,sizeof(v)); ???v[st]=true; ???int head=1,tail=2; ???list[1]=st; ???while(head!=tail) ???{ ???????int x=list[head]; ???????for(int k=last[x];k;k=a[k].next) ???????{ ???????????int y=a[k].y; ???????????if(a[k].c>0&&d[y]>d[x]+a[k].d) ???????????{ ???????????????d[y]=d[x]+a[k].d; ???????????????pos[y]=x; ???????????????pre[y]=k; ???????????????if(v[y]==false) ???????????????{ ???????????????????v[y]=true; ???????????????????list[tail++]=y; ???????????????} ???????????} ???????} ???????head++; ???????v[x]=false; ???} ???if(d[ed]==999999999) return false; ???else return true;}void Flow(){ ???while(spfa()) ???{ ???????ans+=d[ed]; ???????for(int x=ed;x!=st;x=pos[x]) ???????{ ???????????a[pre[x]].c--; ???????????a[a[pre[x]].other].c++; ???????} ???}}int x[5100],y[5100],c[5100],w[5100];int main(){ ???int n,m,k; ???scanf("%d%d%d",&n,&m,&k); ???len=0;memset(last,0,sizeof(last)); ???st=1;ed=n; ???for(int i=1;i<=m;i++) ???{ ???????scanf("%d%d%d%d",&x[i],&y[i],&c[i],&w[i]); ???????ins(x[i],y[i],c[i],0); ???} ???ans=0; ???while(bt_h()==true) ???{ ???????ans+=findflow(st,999999999); ???} ???printf("%d ",ans); ???for(int i=1;i<=m;i++) ins(x[i],y[i],k,w[i]); ???ed=n+1; ???ins(n,n+1,k,0); ???ans=0; ???Flow(); ???printf("%d\n",ans); ???return 0;}
BZOJ1834: [ZJOI2010]network 网络扩容
原文地址:https://www.cnblogs.com/Never-mind/p/8150382.html