【bzoj1834】[ZJOI2010]network 网络扩容
2018-03-19 21:03
405 查看
Description
给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求: 1、在不扩容的情况下,1到N的最大流; 2、将1到N的最大流增加K所需的最小扩容费用。Input
第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。 接下来的M行每行包含四个整数u,v,C,W,表示一条从u到v,容量为C,扩容费用为W的边。N<=1000,M<=5000,K<=10Output
输出文件一行包含两个整数,分别表示问题1和问题2的答案。Sample Input
5 8 21 2 5 8
2 5 9 9
5 1 6 2
5 1 1 8
1 2 8 7
2 5 4 9
1 2 1 1
1 4 2 1
Sample Output
13 19【解析】网络流+费用流第一问很简单,走一遍最大流算法第二问要最小费用最大流,主要是建图,从第一问的残留网络上继续建图,对残留网络上的每一条边建一条容量是999999999费用是w的边然后建个源点,从源点向1建一条容量为k,费用为0的边对新图进行最小费用最大流
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node { int x,y,c,d,t,next,other; }a[51000];int len,last[1100]; int st,ed; void ins(int x,int y,int c,int d) { int k1,k2; len++;k1=len; a[len].x=x;a[len].y=y;a[len].c=c;a[len].t=d; a[len].next=last[x];last[x]=len; len++;k2=len; a[len].x=y;a[len].y=x;a[len].c=0;a[len].t=-d; a[len].next=last[y];last[y]=len; a[k1].other=k2; a[k2].other=k1; } void inss(int x,int y,int c,int d) { int k1,k2; len++;k1=len; a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d; a[len].next=last[x];last[x]=len; len++;k2=len; a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d; a[len].next=last[y];last[y]=len; a[k1].other=k2; a[k2].other=k1; } int list[1100],head,tail,h[1100]; bool bt_h() { memset(h,0,sizeof(h));h[st]=1; list[1]=st;head=1;tail=2; 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 && h[y]==0) { h[y]=h[x]+1; list[tail++]=y; } } head++; } if(h[ed]>0) return true; else return false; } int findflow(int x,int f) { if(x==ed) return f; int t,s=0; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(a[k].c>0 && h[y]==(h[x]+1) && s<f) { t=findflow(y,min(a[k].c,f-s)); s+=t; a[k].c-=t; a[a[k].other].c+=t; } } if(s==0) h[x]=0; return s; } bool v[1100]; int pre[1100],d[1100]; int ans; int n,m,k; bool spfa() { for(int i=0;i<=ed;i++) d[i]=999999999; d[st]=0; memset(v,true,sizeof(v));v[st]=false; list[1]=st;head=1;tail=2; 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; pre[y]=k; if(v[y]==true) { v[y]=false; list[tail++]=y; if(tail==ed+1) tail=1; } } } head++; if(head==ed+1) head=1; v[x]=true; } if(d[ed]>=999999999) return false; return true; } void get() { int minn=999999999,x=ed; while(x!=st) { int k=pre[x]; minn=min(minn,a[k].c); x=a[k].x; } x=ed; while(x!=st) { int k=pre[x]; a[k].c-=minn; a[a[k].other].c+=minn; ans+=minn*a[k].d; x=a[k].x; } } int p[1100]; int main() { 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++) { int x,y,c,dd; scanf("%d%d%d%d",&x,&y,&c,&dd); ins(x,y,c,dd); } int s=0; while(bt_h()==true) s+=findflow(st,999999999); printf("%d ",s); memset(list,0,sizeof(list)); st=n+1; int f=len; for(int i=1;i<=f-1;i+=2) inss(a[i].x,a[i].y,999999999,a[i].t); ins(st,1,k,0); ans=0; while(spfa()==true) get(); printf("%d\n",ans); return 0; }
相关文章推荐
- bzoj1834 [ZJOI2010]network 网络扩容 最大流+费用流
- bzoj 1834 [ZJOI2010] network 网络扩容 题解
- [bzoj1834][ZJOI2010]network 网络扩容
- [网络流] bzoj1834: [ZJOI2010]network 网络扩容
- BZOJ1834: [ZJOI2010]network 网络扩容
- 【BZOJ】【1834】【ZJOI2010】Network 网络扩容
- 【bzoj1834】[ZJOI2010]network 网络扩容
- BZOJ1834 [ZJOI2010]network 网络扩容 【最大流,费用流】
- BZOJ 1834: [ZJOI2010]network 网络扩容
- BZOJ1834 [ZJOI2010]network 网络扩容
- bzoj1834 [ZJOI2010]network 网络扩容
- bzoj 1834: [ZJOI2010]network 网络扩容 网络流+费用流
- bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流
- BZOJ1834 [ZJOI2010] network 网络扩容
- 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)
- 【BZOJ 1834】 [ZJOI2010]network 网络扩容
- 【最大流】【费用流】bzoj1834 [ZJOI2010]network 网络扩容
- [BZOJ1834][ZJOI2010]network 网络扩容 最大流+费用流
- bzoj1834[ZJOI2010]network 网络扩容【最大流+费用流】
- bzoj1834: [ZJOI2010]network 网络扩容