BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)
一看就知道是模板题= = ,不说什么了= =
PS:回去搞期末了,暑假再来刷题了
CODE:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 1010
#define maxm 20010
#define inf 0x7fffffff
using namespace std;
struct edges{
int to,next,cap,dist;bool b;
}edge[maxm];
int n,m,s,t,k,l;
int next[maxn],dist[maxn],way[maxn],h[maxn],p[maxn],gap[maxn];
bool b[maxn];
queue<int> q;
int addedge(int from,int to,int cap,int dist,bool bo){
l++;
edge[l*]=(edges){to,next[from],cap,dist,bo};
edge[l*+]=(edges){from,next[to],,-dist,bo};
next[from]=l*;next[to]=l*+;
return ;
}
bool spfa(){
memset(b,,sizeof(b));
for (int i=;i<=n;i++) dist[i]=inf;
dist[]=;
q.push();
while (!q.empty()){
int u=q.front();q.pop();
b[u]=;
for (int i=next[u];i;i=edge[i].next)
if (edge[i].cap&&dist[edge[i].to]>dist[u]+edge[i].dist) {
dist[edge[i].to]=dist[u]+edge[i].dist;
way[edge[i].to]=i;
if(!b[edge[i].to]){
b[edge[i].to]=;
q.push(edge[i].to);
}
}
}
if (dist[t]==inf) return ;
return ;
}
int mcmf(int cap){
int cost=;
while (spfa()){
int x=n,flow=inf;
while (x!=){
flow=min(flow,edge[way[x]].cap);
x=edge[way[x]^].to;
}
if (flow>=cap) return (cost+=dist[n]*cap);
cost+=dist[n]*flow;
cap-=flow;
x=n;
while(x!=){
edge[way[x]].cap-=flow;
edge[way[x]^].cap+=flow;
x=edge[way[x]^].to;
}
}
return cost;
}
int sap(int u,int flow){
if (u==t) return flow;
int cur=;
for (int i=p[u];i;i=edge[i].next)
if (edge[i].b&&edge[i].cap&&h[u]==h[edge[i].to]+){
int cut=sap(edge[i].to,min(flow-cur,edge[i].cap));
edge[i].cap-=cut;edge[i^].cap+=cut;
p[u]=i;
if ((cur+=cut)==flow ) return flow;
}
if (!(--gap[h[u]])) h[s]=n;
gap[++h[u]]++;
p[u]=next[u];
return cur;
}
int maxflow(){
memset(gap,,sizeof(gap));
memset(h,,sizeof(h));
gap[]=n;
for(int i=;i<=n;i++) p[i]=next[i];
int flow=;
while (h[s]<n) flow+=sap(s,inf);
return flow;
}
int main(){
scanf("%d%d%d",&n,&m,&k);
s=,t=n;
for (int i=;i<=m;i++) {
int x,y,cap,d;
scanf("%d%d%d%d",&x,&y,&cap,&d);
addedge(x,y,cap,,);
addedge(x,y,inf,d,);
}
int ans=maxflow();
printf("%d %d\n",ans,mcmf(k));
return ;
}
|
|
BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)的更多相关文章
- BZOJ 1834 [ZJOI2010]network 网络扩容(费用流)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题目大意] 给定一张有向图,每条边都有一个容量C和一个扩容费用W. 这里扩容费 ...
- bzoj 1834: [ZJOI2010]network 网络扩容【最大流+最小费用最大流】
第一问直接跑最大流即可.建图的时候按照费用流建,费用为0. 对于第二问,在第一问dinic剩下的残量网络上建图,对原图的每条边(i,j),建(i,j,inf,cij),表示可以用c的花费增广这条路.然 ...
- BZOJ_1834_[ZJOI2010]network 网络扩容_费用流
BZOJ_1834_[ZJOI2010]network 网络扩容_费用流 题意: 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的 ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
- bzoj 1834: [ZJOI2010]network 网络扩容 -- 最大流+费用流
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Description 给定一张有向图,每条边都有一个容量C和一 ...
- 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...
- bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题意] 给定一个有向图,每条边有容量C,扩容费用W,问最大流和使容量增加K的最 ...
- bzoj 1834: [ZJOI2010]network 网络扩容
#include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...
- BZOJ 1834: [ZJOI2010]network 网络扩容 最小费用流_最大流_残量网络
对于第一问,跑一遍最大流即可. 对于第二问,在残量网络上的两点间建立边 <u,v>,容量为无限大,费用为扩充费用. 跑一遍最小费用流即可. Code: #include <vecto ...
随机推荐
- 安卓异步任务AsynTask(1)
1.AsynTask类结构asysTask类主要用到的几个内部回调函数有:doInBackGround() onPreExecute() onPostExecute() onProgressUpdat ...
- Nodejs之发送邮件nodemailer
nodejs邮件模块nodemailer的使用说明 1.介绍 nodemailer是node的一个发送邮件的组件,其功能相当强大,普通邮件,传送附件,邮件加密等等都能实现,而且操作也十分方便. nod ...
- bzoj1113
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1113 题解:单调栈 代码: #include<iostream> #includ ...
- Python装饰器学习(九步入门)
这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- coding:gbk -*- '''示 ...
- mySql 分段查询
准备: 创建一个成绩表 Create table grade (id integer, score integer); 插入数据(只有id每次加一,score是1到100的随机数,java生成): p ...
- 【WebStorm】前端工具开发利器webstrom专篇...更新中
http://my.oschina.net/maomi/blog/137807#OSC_h2_5 WebStorm混搭svn WebStorm混搭nodeJS webstorm简单介绍 webstor ...
- 使用(Drawable)资源——LayerDrawable资源
与StateListDrawable有点类似,LayerDrawable也可包含一个Drawable数组,因此系统将会按这些Drawable对象的数组顺序来绘制它们,索引最大的Drawable对象将会 ...
- Delphi隐藏进程
interface function MyHideProcess: Boolean; implementation uses Windows, Classes, AclAPI, accCtrl; ty ...
- origin从图中获得数据
有Origin的原图,即利用Origin的"copy page"功能直接拷贝到文字处理软件的数据图,对于这种图,双击用Origin打开后,双击要导出数据的权限,打开"pl ...
- jQuery插件Flot实战Demo
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...