1834: [ZJOI2010]network 网络扩容

Time Limit: 3 Sec  Memory Limit: 64 MB

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

 先跑一遍最大流
然后在残留网络建边对于每条边重新建一条容量inf的边(注意原边不变且费用为0)
然后就直接跑费用流
 
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 100000007
#define ll long long
#define N 50010
inline int rd()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int T;
int fro[N],to[N],lj[N],v[N],w[N],fa[N],cnt=,tw[N];
void add(int a,int b,int c,int d){fro[++cnt]=lj[a];to[cnt]=b;fa[cnt]=a;v[cnt]=c;tw[cnt]=d;lj[a]=cnt;}
void ins(int a,int b,int c,int d){add(a,b,c,d);add(b,a,,-d);}
int dis[N],q[N],from[N],ans,h,t;
bool vs[N];
bool bfs()
{
memset(dis,,sizeof(dis));
dis[]=q[]=t=;h=;
int tp;
while(h!=t)
{
tp=q[h++]; if(h==N) h=;
for(int i=lj[tp];i;i=fro[i])
{
if(!dis[to[i]]&&v[i])
{
dis[to[i]]=dis[tp]+;
q[t++]=to[i]; if(t==N) t=;
}
}
}
return dis[T]?:;
}
int dfs(int x,int p)
{
if(x==T) return p;
int tp,res=;
for(int i=lj[x];i;i=fro[i])
{
if(v[i]&&dis[to[i]]==dis[x]+)
{
tp=dfs(to[i],min(p-res,v[i]));
v[i]-=tp;
v[i^]+=tp;
res+=tp;
if(res==p) return p;
}
}
if(res==) dis[x]=;
return res;
}
void dinic(){while(bfs()) ans+=dfs(,inf);}
bool spfa()
{
memset(dis,0x3f,sizeof(dis));
int x;
h=q[]=dis[]=;t=vs[]=;
while(h!=t)
{
x=q[h++]; if(h==N) h=;
for(int i=lj[x];i;i=fro[i])
{
if(v[i]&&dis[to[i]]>dis[x]+w[i])
{
dis[to[i]]=dis[x]+w[i];
from[to[i]]=i;
if(!vs[to[i]])
{
vs[to[i]]=;
q[t++]=to[i];if(t==N) t=;
}
}
}
vs[x]=;
}
return dis[T]<inf;
}
void qaz()
{
int tmp=inf;
for(int i=from[T];i;i=from[fa[i]]) tmp=min(tmp,v[i]);
for(int i=from[T];i;i=from[fa[i]])
{
v[i]-=tmp;v[i^]+=tmp;
ans+=w[i]*tmp;
}
}
void add2(int a,int b,int c,int d){fro[++cnt]=lj[a];to[cnt]=b;fa[cnt]=a;v[cnt]=c;w[cnt]=d;lj[a]=cnt;}
void ins2(int a,int b,int c,int d){add2(a,b,c,d);add2(b,a,,-d);}
void build()
{
int tp=cnt;
for(int i=;i<=tp;i+=) ins2(fa[i],to[i],inf,tw[i]);
}
int n,m,k,a,b,c,d;
int main()
{
T=n=rd();m=rd();k=rd();
for(int i=;i<=m;i++)
{
a=rd();b=rd();c=rd();d=rd();
ins(a,b,c,d);
}
dinic();
printf("%d ",ans);
ans=;build();
ins(,,k,);
while(spfa()) qaz();
printf("%d\n",ans);
return ;
}
 

bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流的更多相关文章

  1. BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)

    一看就知道是模板题= = ,不说什么了= = PS:回去搞期末了,暑假再来刷题了 CODE: #include<cstdio> #include<iostream> #incl ...

  2. BZOJ 1834: [ZJOI2010]network 网络扩容 最小费用流_最大流_残量网络

    对于第一问,跑一遍最大流即可. 对于第二问,在残量网络上的两点间建立边 <u,v>,容量为无限大,费用为扩充费用. 跑一遍最小费用流即可. Code: #include <vecto ...

  3. BZOJ 1834 ZJOI2010 network 网络扩展 Dinic+EK费用流

    标题效果:给定一个n积分m无向图边,每一方有一个扩展的成本c.代表扩张1费用的交通,寻求最大流量和扩大的最大流量k最小成本 第一问直接运行的最大流量 第二个问题将是连接到一个流的末端每个边缘的起点是正 ...

  4. BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)

    第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然  后跑最小费用最大流就OK了. ---- ...

  5. BZOJ 1834 [ZJOI2010]network 网络扩容(费用流)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题目大意] 给定一张有向图,每条边都有一个容量C和一个扩容费用W. 这里扩容费 ...

  6. bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】

    第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...

  7. bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题意] 给定一个有向图,每条边有容量C,扩容费用W,问最大流和使容量增加K的最 ...

  8. bzoj 1834: [ZJOI2010]network 网络扩容

    #include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...

  9. 【BZOJ】1834 [ZJOI2010]network 网络扩容

    [算法]网络流-最大流+最小费用最大流(费用流) [题解] 第一问跑最大流. 第二问: 原始边相当于费用为0的边,再原图(跑过最大流的图)基础上添加带费用的边,容量为k(相当于inf). 第一问最大流 ...

随机推荐

  1. MyBatis笔记之配置输出日志并打印输出SQL语句

    1. 引入Log4J的Maven依赖: <dependency> <groupId>log4j</groupId> <artifactId>log4j& ...

  2. Web攻防系列教程之 Cookie注入攻防实战

    摘要:随着网络安全技术的发展,SQL注入作为一种很流行的攻击方式被越来越多的人所知晓.很多网站也都对SQL注入做了防护,许多网站管理员的做法就是添加一个防注入程序.这时我们用常规的手段去探测网站的SQ ...

  3. 123.Best Time to Buy and Sell Stock III---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ 题目大意:与122题类似,只是这 ...

  4. [写出来才有价值系列:node.js]node.js 01-介绍及安装

    对于Node.js在百度百科上是这样解释的: Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V8引 擎执行Javascript的速度 ...

  5. Linux中查看CPU信息 (转)

    cat /proc/cpuinfo中的信息 processor       逻辑处理器的id. physical id    物理封装的处理器的id. core id        每个核心的id. ...

  6. POJ 3087 Shuffle'm Up (模拟+map)

    题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...

  7. csu 1556(快速幂)

    1556: Jerry's trouble Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 787  Solved: 317[Submit][Statu ...

  8. hexdump related.

    hexdump format strings Tue 13 December 2005 In tips. Ian Wienand More from the "things you'd le ...

  9. vs2013设置语言

    设置语言格式 [工具]-[选项]-[国际化]

  10. Roman to Integer & Integer to Roman

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...