bzoj1834 [ZJOI2010]网络扩容
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
13 19
30%的数据中,N<=100
100%的数据中,N<=1000,M<=5000,K<=10
正解:最大流+费用流。
第一问很简单,直接跑最大流即可。第二问,对于每条边,新加一条容量为inf,费用为ci的边,n向汇点连一条容量为ans1+k,费用为0的边。
//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf (1<<30)
#define N ()
#define il inline
#define RG register
#define ll long long
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) using namespace std; struct edge{ int nt,to,flow,cap,cost; }g[]; struct node{ int u,v,w; }G[]; int head[],d[],f[],p[],fa[],q[],n,m,k,flow,cost,ans1,ans2,num=; il int gi(){
RG int x=,q=; RG char ch=getchar(); while ((ch<'' || ch>'') && ch!='-') ch=getchar();
if (ch=='-') q=-,ch=getchar(); while (ch>='' && ch<='') x=x*+ch-,ch=getchar(); return q*x;
} il void insert(RG int from,RG int to,RG int cap,RG int cost){ g[++num]=(edge){head[from],to,,cap,cost},head[from]=num; } il int bfs(RG int S,RG int T){
memset(d,,sizeof(d));
RG int h=,t=; q[t]=S,d[S]=;
while (h<t){
RG int x=q[++h];
for (RG int i=head[x];i;i=g[i].nt){
RG int v=g[i].to;
if (!d[v] && g[i].cap>g[i].flow){
q[++t]=v,d[v]=d[x]+;
if (v==T) return ;
}
}
}
return ;
} il int dfs(RG int x,RG int T,RG int a){
if (x==T || !a) return a; RG int f,flow=;
for (RG int i=head[x];i;i=g[i].nt){
RG int v=g[i].to;
if (d[v]==d[x]+ && g[i].cap>g[i].flow){
f=dfs(v,T,min(a,g[i].cap-g[i].flow));
if (!f){ d[v]=-; continue; }
g[i].flow+=f,g[i^].flow-=f;
flow+=f,a-=f; if (!a) return flow;
}
}
return flow;
} il int maxflow(RG int S,RG int T){ RG int res=; while (bfs(S,T)) res+=dfs(S,T,inf); return res; } il int spfa(RG int S,RG int T){
for (RG int i=;i<=n+;++i) d[i]=inf;
RG int h=,t=; q[t]=S,d[S]=,f[S]=inf;
while (h<t){
RG int x=q[++h];
for (RG int i=head[x];i;i=g[i].nt){
RG int v=g[i].to;
if (d[v]>d[x]+g[i].cost && g[i].cap>g[i].flow){
q[++t]=v,d[v]=d[x]+g[i].cost,fa[v]=x,p[v]=i;
f[v]=min(f[x],g[i].cap-g[i].flow);
}
}
}
if (d[T]==inf) return ; flow+=f[T],cost+=f[T]*d[T];
for (RG int x=T;x!=S;x=fa[x]) g[p[x]].flow+=f[T],g[p[x]^].flow-=f[T];
return ;
} il int mcmf(RG int S,RG int T){ flow=,cost=; while (spfa(S,T)); return cost; } il void work(){
n=gi(),m=gi(),k=gi(); RG int c;
for (RG int i=;i<=m;++i){
G[i].u=gi(),G[i].v=gi(),c=gi(),G[i].w=gi();
insert(G[i].u,G[i].v,c,),insert(G[i].v,G[i].u,,);
}
ans1=maxflow(,n); for (RG int i=;i<=num;++i) g[i].flow=;
for (RG int i=;i<=m;++i) insert(G[i].u,G[i].v,inf,G[i].w),insert(G[i].v,G[i].u,,-G[i].w);
insert(n,n+,ans1+k,),insert(n+,n,,); ans2=mcmf(,n+);
printf("%d %d\n",ans1,ans2); return;
} int main(){
File("network");
work();
return ;
}
bzoj1834 [ZJOI2010]网络扩容的更多相关文章
- BZOJ1834[ZJOI2010]网络扩容——最小费用最大流+最大流
题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小扩容费用 ...
- bzoj1834 ZJOI2010网络扩容(费用流)
给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小扩容费用. 其中\(n ...
- 【BZOJ1834】网络扩容(最大流,费用流)
[BZOJ1834]网络扩容(最大流,费用流) 题面 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下 ...
- 【题解】Luogu P2604 [ZJOI2010]网络扩容
原题传送门:P2604 [ZJOI2010]网络扩容 这题可以说是板题 给你一个图,先让你求最大流 再告诉你,每条边可以花费一些代价,使得流量加一 问至少花费多少代价才能使最大流达到k 解法十分简单 ...
- 洛谷 P2604 [ZJOI2010]网络扩容 解题报告
P2604 [ZJOI2010]网络扩容 题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. ...
- [Luogu 2604] ZJOI2010 网络扩容
[Luogu 2604] ZJOI2010 网络扩容 第一问直接最大流. 第二问,添加一遍带费用的边,边权 INF,超级源点连源点一条容量为 \(k\) 的边来限流,跑费用流. 大约是第一次用 nam ...
- BZOJ1834:[ZJOI2010]网络扩容——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1834 https://www.luogu.org/problemnew/show/P2604#sub ...
- ZJOI2010网络扩容
无限orz hzwer神牛…… 第一问很简单,按数据建图,然后一遍最大流算法即可. 第二问则需要用最小费用最大流算法,主要是建图,那么可以从第一问的残留网络上继续建图,对残留网络上的每一条边建 ...
- 【BZOJ1834】 网络扩容
Time Limit: 1000 ms Memory Limit: 128 MB Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费 ...
随机推荐
- Solr DateRangeField
Solr DateRangeField 是solr5.0 实现的一个feature. 关于 DateRangeField 支持日期查询如 date_field:[2016-03-23 TO 2017 ...
- USB PE
To put WinPE on a USB Stick, you must first make it bootable. Warning: This will destroy all the dat ...
- SQL Server--获取磁盘空间使用情况
对于DBA来说,监控磁盘使用情况是必要的工作,然后没有比较简单的方法能获取到磁盘空间使用率信息,下面总结下这些年攒下的脚本: 最常用的查看磁盘剩余空间,这个属于DBA入门必记的东西: -- 查看磁盘可 ...
- web与request
request --> 封装了客户端所有的请求数据! 请求行 请求头 空行 请求体(GET没体) 回忆一下http协议!请求协议中的数 ...
- Excel公式-求最低价网站名字
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- 老李谈HTTP1.1的长连接 2
HTTP1.1的长连接 但是HTTP1.1开始默认建立的是长连接,即一旦浏览器发起HTTP请求,建立的连接不会请求应答之后立刻断掉. 1. 一个复杂的具备很多HTTP资源的网页会建立多少TCP连接,如 ...
- (转)Java ConcurrentModificationException异常原因和解决方法
转载自:http://www.cnblogs.com/dolphin0520/p/3933551.html 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会 ...
- Spring基础学习(一)—初识Spring
一.Spring的使用 1.导入jar包 2.编写实体类 Person.java public class Person{ private String name; public void say() ...
- Ubuntu常用软件安装(附带地址和卸载自带软件)
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 上次说了安装VSCode(http://www.cnblogs.com/dun ...
- JavaScript高级程序设计---学习笔记(四)
1.全局变量不能通过delete操作符删除,而直接在window对象上定义的属性可以. var age = 29; window.color = "red"; delete age ...