bzoj 1834
网络流的模板题
首先第一问我们直接用dinic搞就行了,费用直接存为0(时间上界非常松,这道题是能过),然后第二问我们只需要在第一问
的残余网络上加一个源点,源点指向1号点,容量为k,费用为0,然后对于之前的每一条边,建一个相同的边
容量为无穷大,费用为原来的费用。
//By BLADEVIL
var
pre, other, len, w :array[..] of longint;
last :array[..] of longint;
l :longint;
n, m, k :longint;
father, que, dis :array[..] of longint;
flag :array[..] of boolean;
a, b, c, d :array[..] of longint;
cost :longint; function min(a,b:longint):longint;
begin
if a>b then min:=b else min:=a;
end; procedure connect(a,b,c,d:longint);
begin
inc(l);
pre[l]:=last[a];
last[a]:=l;
other[l]:=b;
len[l]:=c;
w[l]:=d;
end; procedure init;
var
i :longint; begin
read(n,m,k);
l:=;
for i:= to m do read(a[i],b[i],c[i],d[i]);
for i:= to m do
begin
connect(a[i],b[i],c[i],);
connect(b[i],a[i],,);
end;
end; function bfs:boolean;
var
q, p :longint;
cur :longint;
h, t :longint;
begin
fillchar(dis,sizeof(dis),);
h:=; t:=;
que[]:=; dis[]:=;
while t<>h do
begin
inc(h);
cur:=que[h];
q:=last[cur];
while q<> do
begin
p:=other[q];
if (len[q]>) and (dis[p]=) then
begin
inc(t);
que[t]:=p;
dis[p]:=dis[cur]+;
if p=n then exit(true);
end;
q:=pre[q];
end;
end;
exit(false);
end; function dinic(x,flow:longint):longint;
var
q, p :longint;
rest, tmp :longint;
begin
if x=n then exit(flow);
rest:=flow;
q:=last[x];
while q<> do
begin
p:=other[q];
if (len[q]>) and (rest>) and (dis[p]=dis[x]+) then
begin
tmp:=dinic(p,min(len[q],rest));
dec(rest,tmp);
dec(len[q],tmp);
inc(len[q xor ],tmp);
end;
q:=pre[q];
end;
exit(flow-rest);
end; procedure spfa;
var
q, p :longint;
h, t, cur :longint; begin
fillchar(flag,sizeof(flag),false);
filldword(dis,sizeof(dis) div ,maxlongint div );
h:=; t:=;
que[]:=n+;
dis[n+]:=;
while h<>t do
begin
h:=h mod +;
cur:=que[h];
flag[cur]:=false;
q:=last[cur];
while q<> do
begin
if len[q]> then
begin
p:=other[q];
if dis[cur]+w[q]<dis[p] then
begin
dis[p]:=dis[cur]+w[q];
father[p]:=q;
if not flag[p] then
begin
t:=t mod +;
que[t]:=p;
flag[p]:=true;
end;
end;
end;
q:=pre[q];
end;
end;
end; procedure update;
var
cur :longint;
low :longint;
begin
cur:=n;
low:=maxlongint div ;
while cur<>n+ do
begin
low:=min(low,len[father[cur]]);
cur:=other[father[cur] xor ];
end;
cur:=n;
while cur<>n+ do
begin
inc(cost,low*w[father[cur]]);
dec(len[father[cur]],low);
inc(len[father[cur] xor ],low);
cur:=other[father[cur] xor ];
end;
end; procedure main;
var
ans :longint;
i :longint; begin
ans:=;
while bfs do
ans:=ans+dinic(,maxlongint div ); for i:= to m do
begin
connect(a[i],b[i],maxlongint div ,d[i]);
connect(b[i],a[i],,-d[i]);
end; connect(n+,,k,);
connect(,n+,,);
while true do
begin
spfa;
if dis[n]=maxlongint div then break;
update;
end;
writeln(ans,' ',cost);
end; begin
init;
main;
end.
bzoj 1834的更多相关文章
- BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)
题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...
- bzoj 1834 [ZJOI2010]network 网络扩容(MCMF)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1834 [题意] 给定一个有向图,每条边有容量C,扩容费用W,问最大流和使容量增加K的最 ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
- BZOJ 1834 网络扩容 最大流+最小费用流
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1834 题目大意: 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是 ...
- 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 [题目大意] 给定一张有向图,每条边都有一个容量C和一个扩容费用W. 这里扩容费 ...
- BZOJ 1834 【ZJOI2010】 network 网络扩容
Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的 ...
- bzoj 1834: [ZJOI2010]network 网络扩容
#include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...
- 【BZOJ 1834】 [ZJOI2010]network 网络扩容
Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的 ...
- BZOJ 1834 ZJOI2010 network 网络扩展 Dinic+EK费用流
标题效果:给定一个n积分m无向图边,每一方有一个扩展的成本c.代表扩张1费用的交通,寻求最大流量和扩大的最大流量k最小成本 第一问直接运行的最大流量 第二个问题将是连接到一个流的末端每个边缘的起点是正 ...
随机推荐
- ArcPy:GeoJSON转ArcGIS Geometry
import arcpy geojson = {"type":"Polygon","coordinates":[[[120.81878662 ...
- jmeter使用beanshell构造参数化
1.先在本地写一个java类,用来随机生成一个数字,如: package com.jmeter.test; public class BeanShellTest { public int getRan ...
- Python-学习-小例子练习
网上了点小例子,练习一下下,都是特别简单的.而且这些代码也都是找的网上的代码,目的是在于练习一下Python和熟悉下Python的编码风格等等 学习一门语言,最快的方法就是把它用在世界的开发中,这样才 ...
- Struts2(六.用标签显示用户列表及Value Stack和Stack Context)
一.用Struts2标签显示用户列表 原理: 在struts中可以通过在action中将所有用户的信息存入到某个范围中,然后转向userlist.jsp,进行访问 原则: 在jsp网页上,尽量不要出现 ...
- ASP NET Core ---POST, PUT, PATCH, DELETE,Model 验证
参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/u0765jbwc6f.html 一.POST 安全性和幂等性 安全性是指方法执行后并不会 ...
- python学习总结----简单数据结构
mini-web服务器 - 能够完成简单的请求处理 - 使用http协议 - 目的:加深对网络编程的认识.为后面阶段学习web做铺垫 简单数据结构 - 排列组合 import itertools # ...
- 为DEV C++/CodeBlock配置gdb调试遇到的问题
DEV C++和CodeBlock都只是一个IDE,不能编译调试,需要自己配置MINGW和gdb调试 1.MINGW 在这下载mingw-get-setup.exe安装即可. https://sour ...
- 第十一次ScrumMeeting会议
第十一次ScrumMeeting 时间:2017/11/18 4:00-4:30 地点:主203 人员:全体人员 照片: 工作情况 名字 今日计划 明天的工作 遇到的困难 蔡帜 讨论策划详情\确定WB ...
- SpringBoot 中使用shiro注解使之生效
在shiroConfig配置类中增加如下代码: /** * 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro ...
- 【iOS开发】iOS对UIViewController生命周期和属性方法的解析
iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...