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最小成本 第一问直接运行的最大流量 第二个问题将是连接到一个流的末端每个边缘的起点是正 ...
随机推荐
- VectorDrawable在Android中的配置
一.让Android支持VectorDrawable apply plugin: 'com.android.application' android { defaultConfig { vectorD ...
- 【python模块】——logging
python学习——logging模块
- C++学习014函数值传递和地址传递
当我们给一个函数传参数的时候,可以直接值传入函数,也给可以把一个地址传入函数 区别就是一个本身不被改变,而另一本身也在改变, 在开发时候都会用到, 这里做下记录 #include <iostre ...
- Selenium页面工厂+数据驱动测试框架
工程的目录结构: pom.xml文件: <?xml version="1.0" encoding="UTF-8"?><project xmln ...
- jmeter-maven-plugin
Maven编译JMeter, 使用的是jmeter-maven-plugin插件: <?xml version="1.0" encoding="UTF-8" ...
- MySQL☞length函数
length(字符串/列名):求出该字符串/列名中字符的个数 格式: select length(列名) from 表名 如下图:
- 解析·NOIP·冷门 CLZ最小环
赐予我力量,去改变我所能改变的;赐予我勇气,去接受我不能改变的;并赐予我智慧,去分辨这两者. -----安东尼达斯 NOIP的图论题中有一部分是跟图上的环有关的.蒟蒻的我在USACO上刷题时发现了一种 ...
- Spring实战第八章学习笔记————使用Spring Web Flow
Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...
- Mybatis学习系列(三)动态SQL
在mapper配置文件中,有时需要根据查询条件选择不同的SQL语句,或者将一些使用频率高的SQL语句单独配置,在需要使用的地方引用.Mybatis的一个特性:动态SQL,来解决这个问题. mybati ...
- C#与Javascript变量、函数之间的相互调用
原文地址:http://blog.csdn.net/wonsoft/article/details/2595743 C#与Javascript变量.函数之间的相互调用 一.javascript调用C ...