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最小成本 第一问直接运行的最大流量 第二个问题将是连接到一个流的末端每个边缘的起点是正 ...
随机推荐
- Java测试工具和框架
个人目前只接触过JUnit以及Powermock,后续会关注更多有关测试这方面的东西 8个超实用的Java测试工具和框架_开发/数据库_IT专家网 http://database.ctocio.com ...
- 梳理 Opengl ES 3.0 (二)剖析一个GLSL程序
OpenGL ES shading language 3.0 也被称作 GLSL,是个 C风格的编程语言. Opengl ES 3.0内部有两种可编程处理单元,即Vertex processor和Fr ...
- P4332三叉神经树
题面 \(Solution\) 通过模拟,我们会发现每次修改 \(x\),只会改变从 \(x\) 向上一段连续的链的输出. 例如将 \(x\) 点从 \(0\) 改为 \(1,\) 那么它会影响从它向 ...
- Leetcode 686.重复叠加字符串匹配
重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd", ...
- cmp快排 结构体快排
由于深陷于JAVA的面向对象思想,常常会用到结构体,记一下这个模板,方便直接调用进行结构体排序: struct point { int val,turn; }; bool cmp(struct poi ...
- 并查集——poj1611(入门)
传送门:The Suspects 并查集水题 #include <iostream> #include <cstdio> #include <algorithm> ...
- nopcommerce商城系统--开发者常遇问题清单
原址:http://www.nopcommerce.com/docs/74/frequently-asked-development-questions.aspx 以下是开发者常见问题的清单.也介绍了 ...
- arm单板上移植gdb
虚拟机 : vmware 12 image: ubuntukylin 14.04.1 系统版本:Linux dancy 3.13.0-32-generic #57-Ubuntu SMP Tue Jul ...
- 一个python游戏源码
#finalPyPong.py import pygame,sys class MyBallClass(pygame.sprite.Sprite): def __init__(self,image_f ...
- sql数值比较