【题目链接】

http://www.lydsy.com/JudgeOnline/problem.php?id=1834

【题意】

给定一个有向图,每条边有容量C,扩容费用W,问最大流和使容量增加K的最少扩容费用。

【思路】

第一问就是费用为0的费用流

第二问在第一问的残量网络上操作,对于每条边都新加一条容量为inf,且费用为W的边。至于容量增加K,只要新建一个S点向1连一条容量为K费用为0的边即可。然后跑一遍最小费用最大流。

【代码】

 #include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define trav(u,i) for(int i=front[u];i;i=e[i].nxt)
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long ll;
const int N = 1e3+;
const int inf = 2e9; ll read() {
char c=getchar();
ll f=,x=;
while(!isdigit(c)) {
if(c=='-') f=-; c=getchar();
}
while(isdigit(c))
x=x*+c-'',c=getchar();
return x*f;
} struct Edge { int u,v,cap,flow,cost,t;
}; struct MCMF {
int n,m,s,t;
int a[N],inq[N],d[N],p[N];
queue<int> q;
vector<Edge> es;
vector<int> g[N];
void init(int n) {
this->n=n;
es.clear();
for(int i=;i<=n;i++) g[i].clear();
}
void AddEdge(int u,int v,int w,int c,int f) {
es.push_back((Edge){u,v,w,,f*c,c});
es.push_back((Edge){v,u,,,-f*c,c});
int m=es.size();
g[u].push_back(m-);
g[v].push_back(m-);
}
bool spfa(int s,int t,int& flow,int& cost) {
memset(inq,,sizeof(inq));
for(int i=;i<=n;i++) d[i]=inf;
q.push(s);
inq[s]=,a[s]=inf,d[s]=;
while(!q.empty()) {
int u=q.front(); q.pop();
inq[u]=;
for(int i=;i<g[u].size();i++) {
Edge& e=es[g[u][i]];
int v=e.v;
if(e.cap>e.flow && d[v]>d[u]+e.cost) {
d[v]=d[u]+e.cost;
p[v]=g[u][i];
a[v]=min(a[u],e.cap-e.flow);
if(!inq[v])
inq[v]=,
q.push(v);
}
}
}
if(d[t]==inf) return ;
flow+=a[t],cost+=a[t]*d[t];
for(int x=t;x!=s;x=es[p[x]].u) {
es[p[x]].flow+=a[t];
es[p[x]^].flow-=a[t];
}
return ;
}
void mcmf(int s,int t,int& flow,int& cost) {
flow=cost=;
while(spfa(s,t,flow,cost)) ;
}
} mc; int n,m,K; int main()
{
n=read(),m=read(),K=read();
mc.init(n+);
int S=,T=n;
int u,v,w,c;
FOR(i,,m) {
u=read(),v=read(),w=read(),c=read();
mc.AddEdge(u,v,w,c,);
}
int flow,cost;
mc.mcmf(,n,flow,cost);
printf("%d ",flow);
int mx=mc.es.size();
for(int i=;i<mx;i+=) {
Edge e=mc.es[i];
mc.AddEdge(e.u,e.v,inf,e.t,);
}
mc.AddEdge(S,,K,,);
mc.mcmf(S,T,flow,cost);
printf("%d",cost);
return ;
}

bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)的更多相关文章

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

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

  2. bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec  Memory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一 ...

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

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

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

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

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

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

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

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

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

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

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...

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

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

随机推荐

  1. The 6th Zhejiang Provincial Collegiate Programming Contest->ProblemA:Second-price Auction

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3202 题意:拍卖东西,以第二高价的价格卖给出第一高价的人.输出最后获得东西 ...

  2. Educational Codeforces Round 5 B

    Problem B:http://codeforces.com/contest/616/problem/B B. Dinner with Emma 题意:一对夫妻要去餐厅吃晚饭,Emma 想去最豪华( ...

  3. Samza在YARN上的启动过程 =》 之一

    运行脚本,提交job 往YARN提交Samza job要使用run-job.sh这个脚本. samza-example/target/bin/run-job.sh  --config-factory= ...

  4. properties配置文件中文乱码解决方法

    方法1   properties文件的格式一般为: ROOT=http://localhost:8080/BNCAR2/ ROOTPATH=E:/ws2/BNCAR2/rel/ MALL_PARTS_ ...

  5. Executing a script from Nagios event handler fails to run

    I have Nagios running on a webserver. For this one Nagios service check in particular, if it fails, ...

  6. HDMI介绍与流程

    HDMI接口 http://baike.c114.net/view.asp?id=17671-21565442 DDC(Display Data Channel)通道用于HDMI发送和接收端之间交换一 ...

  7. Ubuntu中安装DiscuzX2

    http://blog.csdn.net/kevin_ysu/article/details/7452938 一.Apache的安装 Apache作为一个功能强大的Web程序,自然是架建Web服务器的 ...

  8. 【流媒體】live555—VS2008 下live555编译、使用及测试

    [流媒體]live555—VS22008 下live555编译.使用及测试 Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如R ...

  9. python学习笔记四--元组

    一.元组: 1. 不可变更的列表 2. 从语法上,她们是编写在小括号里,不是方括号里,列表是编写在方括号里的 3. 圆括号也同时用于表达式,如果想说明这是一个元组,不是表达式,可以在value后,关闭 ...

  10. Android 签名(8)签名前用Zipalign简单优化

    1 为什么要优化 Android SDK中包含一个“zipalign”的工具,它能够对打包的应用程序进行优化.在你的应用程序上运行zipalign,使得在运行时Android与应用程序间的交互更加有效 ...