bzoj1930
一开始我觉得这不是一个弱弱的费用流吗?
每个豆豆拆点,入点出点随便连连
由于肯定是DAG图,边权为正的最大费用肯定能增广出来
于是我们只要跑总流量为2的最大费用最大流不就行了吗
但是
这样会TLE,因为会出现稠密图,spfa跑稠密图太慢
既然这样,能不能换一个找增广路的方法呢?
最大路径更快的方法就是DAG图的拓扑排序+dp
但好像无法处理反向弧导致带来的环啊
那既然这样,反正总流量很小,
我们可以用DAG解决最大路径特有的dp找第一条增广路(因为这时候是肯定的DAG),跑出一个较大费用
然后再用spfa增广呢?这样就大大减小了时间复杂度
这就是AC方法
注意这道题空间卡的较紧,能开integer不要开longint
实现起来要小心
const mo=;
type node=record
next:longint;
point,flow,cost:integer;
end; var edge:array[..] of node;
v:array[..] of boolean;
d,rd,pre,pref:array[..] of integer;
q:array[..] of integer;
p,x,y,cur:array[..] of longint;
len:longint;
max,s,t,i,j,n,m,be:integer; procedure add(x,y,f,c:integer);
begin
edge[len].point:=y;
edge[len].flow:=f;
edge[len].cost:=c;
edge[len].next:=p[x];
p[x]:=len;
end; procedure preflow;
var x,y,f,r,i,j:longint;
begin
r:=;
f:=;
q[]:=s;
while f<=r do //拓扑排序
begin
x:=q[f];
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
dec(rd[y]);
if (rd[y]=) then
begin
inc(r);
q[r]:=y;
end;
i:=edge[i].next;
end;
inc(f);
end;
fillchar(d,sizeof(d),);
d[s]:=;
for j:= to r do //DAG图根据拓扑的顺序dp解决最大路径
begin
x:=q[j];
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
if d[y]<d[x]+ then
begin
d[y]:=d[x]+;
pre[y]:=x;
cur[y]:=i;
if d[y]>d[max] then max:=y;
end;
i:=edge[i].next;
end;
end;
end; procedure change; //建立预处理后的网络继续增广
var i,j,e,w:longint;
begin
for i:= to n do
pref[i]:=;
i:=max;
while i<>s do //相当于找到第一条增广路然后弄一弄反向弧什么的
begin
pref[i]:=;
j:=cur[i];
dec(edge[j].flow);
if pre[i]=s then w:=i;
i:=pre[i];
end;
len:=-;
for i:= to n do
for j:= to n do
if (i<>j) then
if (x[i]<=x[j]) and (y[i]<=y[j]) then
begin
inc(len);
edge[len].point:=j+n;
inc(len);
add(j+n,i,-edge[len-].flow,) //添加原来没加的反向弧
end; p[s]:=-;
for i:= to n do //下面很多处理的细节,不赘述,繁杂但不难想到
begin
if i=w then e:= else e:=;
inc(len);
add(s,i+n,e,);
inc(len);
add(i+n,s,-e,);
end;
// 表示超级源点,s表示起点,t表示超级汇点
t:=*n+;
inc(len);
add(,s,,);
inc(len);
add(s,,,);
for i:= to n do
begin
inc(len);
add(i+n,i,pref[i],);
inc(len);
add(i,i+n,-pref[i],-);
if max=i then e:=
else e:=;
inc(len);
add(i,t,e,);
inc(len);
add(t,i,-e,);
end;
end; function spfa:boolean;
var f,r,x,y,i,j,head,tail:longint;
begin
fillchar(v,sizeof(v),false);
for i:= to t do
d[i]:=-mo;
d[]:=;
q[]:=;
f:=;
r:=;
head:=;
tail:=;
while head<=tail do //缩空间的写法,好久没这么写了
begin
x:=q[f];
v[x]:=false;
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
if edge[i].flow> then
begin
if d[x]+edge[i].cost>d[y] then
begin
d[y]:=edge[i].cost+d[x];
pre[y]:=x;
cur[y]:=i;
if not v[y] then
begin
v[y]:=true;
r:=(r+) mod mo;
inc(tail);
q[r]:=y;
end;
end;
end;
i:=edge[i].next;
end;
f:=(f+) mod mo;
inc(head);
end;
if d[t]=-mo then exit(false) else exit(true);
end; function mincost:longint; //最大费用最大流
var i,j:longint;
begin
be:=d[max];
mincost:=be; //预处理出来的费用
while spfa do
begin
mincost:=mincost+d[t];
i:=t;
while i<> do
begin
j:=cur[i];
dec(edge[j].flow);
inc(edge[j xor ].flow);
i:=pre[i];
end;
end;
end; begin
len:=-;
fillchar(p,sizeof(p),);
readln(n);
for i:= to n do
readln(x[i],y[i]);
for i:= to n do
for j:= to n do
if (i<>j) then
if (x[i]<=x[j]) and (y[i]<=y[j]) then
begin
len:=len+;
add(i,j,,); //个人感觉这样建之后添加边更容易一点
inc(rd[j]);
end; s:=*n+;
for i:= to n do
if rd[i]= then
begin
len:=len+;
add(s,i,,);
inc(rd[i]);
end; preflow;
change;
writeln(mincost);
end.
bzoj1930的更多相关文章
- 【BZOJ1930】[Shoi2003]pacman 吃豆豆 最大费用最大流
[BZOJ1930][Shoi2003]pacman 吃豆豆 Description 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会 ...
- BZOJ1930 [Shoi2003]pacman 吃豆豆
dp,首先建出图,f[i][j]表示a吃到了i点,b吃到了j点的最大值,转移的时候转移拓扑序小的那一维,如果i拓扑序小于j,那么转移到f[k][j],否则转移到f[i][k],建出的图边数也要优化, ...
- 【BZOJ1930】【SHOI2003】吃豆豆
初见杀…… 原题: 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会吃掉它.PACMAN行走的路线很奇怪,只能向右走或者向上走,他们行 ...
- [转载]hzwer的bzoj题单
counter: 664BZOJ1601 BZOJ1003 BZOJ1002 BZOJ1192 BZOJ1303 BZOJ1270 BZOJ3039 BZOJ1191 BZOJ1059 BZOJ120 ...
- BZOJ刷题列表【转载于hzwer】
沿着黄学长的步伐~~ 红色为已刷,黑色为未刷,看我多久能搞完吧... Update on 7.26 :之前咕了好久...(足见博主的flag是多么emmm......)这几天开始会抽时间刷的,每天几道 ...
随机推荐
- Go与GUI——GO语言的图形界面Walk
GO没有原生的界面库,所以不能直接用来写GUI界面.但最近互联网上已经涌现出不少成熟.好用的第三方界面库.使用它们,就同样可以写出同C#.C++的界面.而且效率还更胜一筹. 关于Walk界面库(官方介 ...
- C++ 中的“ !” 运算
在介绍“ !”运算之前,我们要知道一个变量n,如果n>0,那么我们可以在逻辑上叫它“真”,如果n<=0 ,那么我们可以在逻辑上叫它“假”. n为真时,!n就为假(false),转换为整型值 ...
- Sicily 1510欢迎提出优化方案
这道题我觉得是除1000(A-B)外最简单的题了……不过还是提出一个小问题:在本机用gcc编译的时候我没包括string.h头文件,通过编译,为什么在sicily上却编译失败? 1510. Mispe ...
- 九度OJ 1104 整除问题
题目地址:http://ac.jobdu.com/problem.php?pid=1104 题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2 ...
- 【应用】:shell crontab定时生成oracle表的数据到txt文件,并上传到ftp
一.本人环境描述 1.oracle服务端装在win7 32位上,oracle版本为10.2.0.1.0 2.Linux为centos6.5 32位,安装在Oracle VM Vir ...
- jQuery 选择器(转)
jQuery 选择器 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元 ...
- IOC----LightInject
开源项目 引入 LightInject.cs 默认服务 new ServiceContainer 注册跟获取获取服务 container.Register<IFoo, Foo>();con ...
- hdu5548
2015ACM/ICPC亚洲区上海站LCM WALK 题意:定义了一种走法,就是从当前的点为sx,sy,可以走到ex,ey;并且ex = sx + z,或者 ey = sy + z, 其中z为lcm( ...
- Lucene基础(四)-- 结合数据库使用
需求 很多时候我们在用数据库的需要使用模糊查询,我们一般会使用like语句来做,然而这样的做的效率不是很多(很抱歉我们亲自去测,很多都这么说的),那么使用Lucene来检索的话,效率会高很多. luc ...
- iOS实现地图半翻页效果--老代码备用参考
// Curl the image up or down CATransition *animation = [CATransition animation]; [animation setDurat ...