BZOJ1834 [ZJOI2010]network 网络扩容
2017-12-08 10:31
330 查看
标签:最大流,费用流
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 2
1 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
HINT
Source
Day1
第二问需要用最小费用最大流的算法,建立超级源点(0),从超级源点向1号点建立一条容量为k,费用为0的边,再在原来的所有边上都新开一条费用为w,容量为∞的边,对这个图进行最小费用最大流的算法
题目
题目传送门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 2
1 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
30%的数据中,N<=100 100%的数据中,N<=1000,M<=5000,K<=10
HINT
Source
Day1
分析
第一问是最大流的模板题第二问需要用最小费用最大流的算法,建立超级源点(0),从超级源点向1号点建立一条容量为k,费用为0的边,再在原来的所有边上都新开一条费用为w,容量为∞的边,对这个图进行最小费用最大流的算法
code
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #define rep(i,a,b) for(register int i=a;i<=b;i++) #define dep(i,a,b) for(register int i=a;i>=b;i--) #define reg(x) for(int i=last[x];i;i=e[i].next) #define ll long long #define mem(x,num) memset(x,num,sizeof(x)) #define inf 0x7fffffff using namespace std; inline ll read() { ll f=1,x=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } const int maxn=1e4+6,maxq=1e5+6; struct edge{int to,from,v,t,next,c;}e[maxn<<1]; int n,m,k,cnt=1,ans,last[maxn],from[maxn],que[maxq],h[maxq],dis[maxn],inq[maxn]; bool bfs() { int head=0,tail=0,now; mem(h,-1);que[0]=1,h[1]=0; while(head<=tail){ now=que[head++]; reg(now) if(e[i].v&&h[e[i].to]==-1){h[e[i].to]=h[now]+1;que[++tail]=e[i].to;} } if(h ==-1)return 0;else return 1; } int dfs(int x,int f) { if(x==n)return f; int w,used=0; reg(x) if(e[i].v&&h[e[i].to]==h[x]+1){ w=f-used;w=dfs(e[i].to,min(w,e[i].v)); e[i].v-=w;e[i^1].v+=w; used+=w;if(used==f)return f; } if(!used)h[x]=-1; return used; } void dinic(){while(bfs())ans+=dfs(1,inf);} void build() { int t=cnt; rep(i,2,t) if(i%2==0){ e[++cnt]=(edge){e[i].to,e[i].from,inf,0,last[e[i].from],e[i].t};last[e[i].from]=cnt; e[++cnt]=(edge){e[i].from,e[i].to,0,0,last[e[i].to],-e[i].t};last[e[i].to]=cnt;} } bool spfa() { int head=0,tail=0,now; rep(i,0,n)dis[i]=inf; que[0]=dis[0]=0;inq[0]=1; while(head<=tail){ now=que[head++]; reg(now) if(e[i].v&&dis[now]+e[i].c<dis[e[i].to]){ dis[e[i].to]=dis[now]+e[i].c;from[e[i].to]=i; if(!inq[e[i].to]){que[++tail]=e[i].to;inq[e[i].to]=1;} } inq[now]=0; } if(dis ==inf)return 0; return 1; } void mcf() { int x=inf; for(int i=from ;i;i=from[e[i].from])x=min(x,e[i].v); for(int i=from ;i;i=from[e[i].from]){e[i].v-=x;e[i^1].v+=x;ans+=x*e[i].c;} } int main() { n=read(),m=read(),k=read(); rep(i,1,m){ int u=read(),v=read(),w=read(),c=read(); e[++cnt]=(edge){v,u,w,c,last[u]};last[u]=cnt; e[++cnt]=(edge){u,v,0,-c,last[v]};last[v]=cnt; } dinic(); cout<<ans<<' '; ans=0;build(); e[++cnt]=(edge){1,0,k,0,last[0],0};last[0]=cnt; while(spfa())mcf(); cout<<ans<<endl; return 0; }
相关文章推荐
- bzoj1834: [ZJOI2010]network 网络扩容(最小费用最大流)
- BZOJ1834: [ZJOI2010]network 网络扩容
- bzoj 1834: [ZJOI2010]network 网络扩容(isap+费用流)
- 【bzoj1834】[ZJOI2010]network 网络扩容
- bzoj1834 [ZJOI2010]network 网络扩容
- 1834: [ZJOI2010]network 网络扩容
- BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)
- bzoj 1834: [ZJOI2010]network 网络扩容
- 【bzoj1834】[ZJOI2010]network 网络扩容
- 【ZJOI2010】network-网络扩容
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
- Bzoj:[ZJOI2010]network 网络扩容:网络流,最大流+费用流
- BZOJ 1834 ZJOI2010 network 网络扩容 Dinic+EK费用流
- bzoj1834 [ZJOI2010]network 网络扩容 最大流+费用流
- [ZJOI2010]network 网络扩容
- bzoj 1834 [ZJOI2010] network 网络扩容(费用流)
- BZOJ_1834_[ZJOI2010]network 网络扩容_费用流
- bzoj 1834: [ZJOI2010]network 网络扩容 网络流+费用流
- BZOJ 1834: [ZJOI2010]network 网络扩容
- 【BZOJ1834】[ZJOI2010]network 网络扩容 最大流+最小费用流