BZOJ 1834: [ZJOI2010]network 网络扩容
2014-06-05 23:06
176 查看
Description
给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求: 1、 在不扩容的情况下,1到N的最大流; 2、 将1到N的最大流增加K所需的最小扩容费用。Input
输入文件的第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。 接下来的M行每行包含四个整数u,v,C,W,表示一条从u到v,容量为C,扩容费用为W的边。Output
输出文件一行包含两个整数,分别表示问题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 1930%的数据中,N<=100
100%的数据中,N<=1000,M<=5000,K<=10
题解
最大流+费用流。第一问最大流即可。
第二问为“最小费用最大流”。
由题意,这一问的可转化为在上一问的“残量网络”上,扩大一些边的容量,使能从新的图中的最大流为k。
那么易得:对于还有剩余流量的边,走过他们的费用为0。而“增加流量”可变为:对残留网络上的每一条边建一条容量是∞费用是w的边。这表示从这些边走,每一流量的费用为w,这就符合题意了。
最后建一个超级源点,从超级源向1建一条容量为k,费用为0的边,就可进行最小费用最大流算法。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> #define inf 0x7fffffff using namespace std; int n,m,K,head[1005],zz=1; struct bian {int frm,to,v,nx,k,t;} e[50002];//t用来暂存费用 int q[1005],h[1005],ans; int dis[1005],pd[1005],fr[1005]; void insert(int x,int y,int z,int k) { zz++; e[zz].frm=x; e[zz].to=y; e[zz].v=z; e[zz].t=k; e[zz].nx=head[x]; head[x]=zz; zz++; e[zz].frm=y; e[zz].to=x; e[zz].v=0; e[zz].t=-k; e[zz].nx=head[y]; head[y]=zz; } void init() { scanf("%d%d%d",&n,&m,&K); for(int i=1;i<=m;i++) {int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); insert(a,b,c,d); } } bool bfs() { memset(h,-1,sizeof(h)); int t=0,w=1; q[0]=1; h[1]=0; while(t<w) {int p=q[t],i; t++; i=head[p]; while(i) {if(e[i].v&&h[e[i].to]==-1) {h[e[i].to]=h[p]+1; q[w++]=e[i].to; } i=e[i].nx; } } if(h ==-1) return false; return true; } int dfs(int x,int f) { if(x==n) return f; int rest,usd=0,i=head[x]; while(i) {if(e[i].v&&h[e[i].to]==h[x]+1) {rest=f-usd; rest=dfs(e[i].to,min(rest,e[i].v)); e[i].v-=rest; e[i^1].v+=rest; usd+=rest; if(usd==f) return f; } i=e[i].nx; } if(!usd) h[x]=-1; return usd; } void dinic() {while(bfs()) ans+=dfs(1,inf);} void rebuild(int x,int y,int z,int k) { zz++; e[zz].frm=x; e[zz].to=y; e[zz].v=z; e[zz].k=k; e[zz].nx=head[x]; head[x]=zz; zz++; e[zz].frm=y; e[zz].to=x; e[zz].v=0; e[zz].k=-k; e[zz].nx=head[y]; head[y]=zz; } void build() { int t=zz; for(int i=2;i<=t;i+=2) rebuild(e[i].frm,e[i].to,inf,e[i].t); rebuild(0,1,K,0); } bool spfa() { for(int i=0;i<=n;i++) dis[i]=inf; int t=0,w=1; dis[0]=q[0]=0; pd[0]=1; while(t!=w) {int p=q[t],i; t=(t+1)%n; i=head[p]; while(i) {if(e[i].v&&dis[p]+e[i].k<dis[e[i].to]) {dis[e[i].to]=dis[p]+e[i].k; fr[e[i].to]=i; if(!pd[e[i].to]) {q[w]=e[i].to; w=(w+1)%n; pd[e[i].to]=1;} } i=e[i].nx; } pd[p]=0; } if(dis ==inf) return false; return true; } void getf() { int i,x=inf; i=fr ; while(i) {x=min(x,e[i].v); i=fr[e[i].frm];} i=fr ; while(i) {e[i].v-=x; e[i^1].v+=x; ans+=x*e[i].k; i=fr[e[i].frm]; } } void mcf() {while(spfa()) getf();} int main() { init(); dinic(); printf("%d ",ans); ans=0; build(); mcf(); printf("%d",ans); return 0; }
相关文章推荐
- bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)
- bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流
- BZOJ1834 [ZJOI2010]network 网络扩容(最小费用最大流)
- 1834: [ZJOI2010]network 网络扩容
- 【bzoj1834】 network 网络扩容 【ZJOI2010】
- 834: [ZJOI2010]network 网络扩容
- 【BZOJ 1834】 [ZJOI2010]network 网络扩容
- BZOJ_1834_[ZJOI2010]network 网络扩容_费用流
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
- 【bzoj1834】[ZJOI2010]network 网络扩容
- bzoj 1834 [ZJOI2010] network 网络扩容(费用流)
- bzoj1834 [ZJOI2010]network 网络扩容 最大流+费用流
- BZOJ 1834: [ZJOI2010]network 网络扩容
- bzoj1834[ZJOI2010]network 网络扩容【最大流+费用流】
- 【bzoj1834】[ZJOI2010]network 网络扩容
- ZJOI2010——network 网络扩容(费用流)
- 【bzoj1834】[ZJOI2010]network 网络扩容 最大流+最小费用流
- 【bzoj1834】[ZJOI2010]network 网络扩容 费用流
- BZOJ1834: [ZJOI2010]network 网络扩容
- 1834: [ZJOI2010]network 网络扩容 (最小费用最大流模板)