[BZOJ1834][ZJOI2010]network 网络扩容 最大流+费用流
1834: [ZJOI2010]network 网络扩容
Time Limit: 3 Sec Memory Limit: 64 MB Submit: 3330 Solved: 1739 [Submit][Status][Discuss]
Description
Input
Output
Sample Input
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
30%的数据中,N<=100
100%的数据中,N<=1000,M<=5000,K<=10
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define maxm 5005
#define maxn 1005
using namespace std;
struct data {
int from,to,next,w,c;
}e[maxm*];
int head[maxn],cnt;
void add(int u,int v,int w,int c){e[cnt].from=u;e[cnt].next=head[u];e[cnt].to=v;e[cnt].w=w;e[cnt].c=c;head[u]=cnt++;}
int n,m,k;
int q[maxn];
bool vis[maxn];
int dis[maxn];
bool bfs() {
memset(dis,-,sizeof(dis));
int h=,t=;
q[h]=;
vis[]=;
dis[]=;
while(h!=t) {
int now=q[h];h++;vis[now]=;if(h==) h=;
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
if(e[i].w&&dis[to]<) {
dis[to]=dis[now]+;
if(!vis[to]){
vis[to]=;
q[t++]=to;if(t==)t=;
}
}
}
}
return dis[n]!=-;
}
int dfs(int now,int a) {
if(now==n||a==) return a;
int flow=,f;
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
if(dis[to]==dis[now]+&&e[i].w>) {
f=dfs(to,min(a,e[i].w));
e[i].w-=f;
e[i^].w+=f;
flow+=f;
a-=f;
if(a==) return flow;
}
}
if(!flow) dis[now]=-;
return flow;
}
void work1() {
int ans=;
while(bfs()){ans+=dfs(,);}
printf("%d ",ans);
}
int cost=;
bool spfa() {
for(int i=;i<=n;i++) dis[i]=-;
int h=,t=;
q[h]=n;
vis[n]=;
dis[n]=;
while(h!=t) {
int now=q[h];h++;vis[now]=;if(h==) h=;
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
if(e[i^].w&&dis[to]<dis[now]+e[i].c) {
dis[to]=dis[now]+e[i].c;
if(!vis[to]){
vis[to]=;
q[t++]=to;if(t==)t=;
}
}
}
}
cost-=dis[];
return dis[]!=-;
}
int ans2;
int zkw(int now,int a) {
if(now==n||a==){ans2+=cost*a;return a;}
int flow=,f;vis[now]=;
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
if(dis[to]==dis[now]+e[i].c&&e[i].w>&&!vis[to]&&(f=zkw(to,min(a,e[i].w)))) {
e[i].w-=f;
e[i^].w+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
void build() {
int t=cnt;
for(int i=;i<t;i+=) {
add(e[i].from,e[i].to,,e[i].c);
add(e[i].to,e[i].from,,-e[i].c);
}
add(,,k,);
add(,,,);
for(int i=;i<t;i++) e[i].c=;
}
void work2() {
ans2=;cost=;
build();
memset(vis,,sizeof(vis));
while(spfa()) {
do {
memset(vis,,sizeof(vis));
}while(zkw(,));
memset(vis,,sizeof(vis));cost=;
}
printf("%d",ans2);
}
int main() {
memset(head,-,sizeof(head));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++) {
int u,v,w,c;
scanf("%d%d%d%d",&u,&v,&w,&c);
add(u,v,w,c);add(v,u,,-c);
}
work1();
work2();
}
[BZOJ1834][ZJOI2010]network 网络扩容 最大流+费用流的更多相关文章
- BZOJ1834 [ZJOI2010]network 网络扩容(最小费用最大流)
挺直白的构图..最小费用最大流的定义. #include<cstdio> #include<cstring> #include<queue> #include< ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)
一看就知道是模板题= = ,不说什么了= = PS:回去搞期末了,暑假再来刷题了 CODE: #include<cstdio> #include<iostream> #incl ...
- BZOJ1834 [ZJOI2010]network 网络扩容 【最大流,费用流】
1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 3394 Solved: 1774 [Subm ...
- 【最大流】【费用流】bzoj1834 [ZJOI2010]network 网络扩容
引用题解: 最大流+费用流. 第一问最大流即可. 第二问为“最小费用最大流”. 由题意,这一问的可转化为在上一问的“残量网络”上,扩大一些边的容量,使能从新的图中的最大流为k. 那么易得:对于还有剩余 ...
- bzoj1834: [ZJOI2010]network 网络扩容 费用流
bzoj1834 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小扩容 ...
- 2018.10.13 bzoj1834: [ZJOI2010]network 网络扩容(最大流+费用流)
传送门 网络流水题啊. 第一问直接放心跑最大流(本来还以为有什么tricktricktrick). 第二问就直接把原来的边(u,v,c,w)(u,v,c,w)(u,v,c,w)变成(u,v,c,0)( ...
- bzoj1834 [ZJOI2010]network 网络扩容
第一问跑最大流,第二问新建一条边连接0和1,流量为上第一问的答案+k,费用为0,接下来图中每条边拆成两条边,第一条容量为C费用为0,第二条容量无穷费用为W,再跑一遍费用流即可. 代码 #include ...
- 【费用流】bzoj1834: [ZJOI2010]network 网络扩容
还是稍微记一下这个拆点模型吧 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: ...
- BZOJ 1834: [ZJOI2010]network 网络扩容 最小费用流_最大流_残量网络
对于第一问,跑一遍最大流即可. 对于第二问,在残量网络上的两点间建立边 <u,v>,容量为无限大,费用为扩充费用. 跑一遍最小费用流即可. Code: #include <vecto ...
随机推荐
- 开启虚拟机所报的错误:VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the program, access all directories the program uses, and access all directories for temporary fil
当我们开启虚拟机时出现错误: VMware Workstation cannot connect to the virtual machine. Make sure you have rights t ...
- centos使用--vim配置和推荐插件使用
目录 1.vimrc的配置内容 2.Vundle使用 简介 安装vundle 配置vundle插件: 安装需要的插件 移除不需要的插件 其他常用命令 3 使用插件 3.1 NERDTree 3.2 e ...
- Java SE 5.0 - SE 8 的功能增强
Table of Contents 前言 Java 5.0 Generics Enhanced for Loop Autoboxing Typesafe Enums Varargs Static Im ...
- Python全栈工程师(迭代器、字节串)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 Python人工智能从入门到精通 迭代器 Iterator: 用<&g ...
- [C++] Const详解
/**************************************************************** 初级理解: 1.const是定义常量 ==>const意味着只 ...
- Python数据分析-Pandas(Series与DataFrame)
Pandas介绍: pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. Pandas的主要功能: 1)具备对其功能的数据结构DataFrame.Series 2)集成时间序 ...
- 201621123034 《Java程序设计》第11周学习总结
作业11-多线程 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread ...
- MSHflexgrid控件删除选中行
相应的代码: Private Sub some_Click() '定义变量 Dim txtSQL As String Dim MsgText As String Dim Online_mrc As A ...
- Spring 对数据库的支持
DAO DAO是用于访问数据的对象,大多数时候,我们将数据保存在数据库中,但这不是唯一选择. 用户也可以将数据保存在数据文件或者LDAP中 DAO屏蔽了数据操作的具体细节 Spring本质上希望能够以 ...
- [洛谷P4656][CEOI2017]Palindromic Partitions
题目大意:一个长度为$n$的字符串,要求把它分成尽可能多的小块,使得这些块构成回文串 题解:贪心,从两边从找尽可能小的块使得左右的块相等,判断相等可以用$hash$ 卡点:无 C++ Code: #i ...