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......)这几天开始会抽时间刷的,每天几道 ...
随机推荐
- iso和Android的验证码输入框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [leetcode] 399. Evaluate Division
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
- bzoj1015:[JSOI2008]星球大战starwar
思路:反着做用并查集维护连通块个数就好了. #include<iostream> #include<cstdio> #include<cstring> #inclu ...
- 九度OJ 1446 Head of a Gang -- 并查集
题目地址:http://ac.jobdu.com/problem.php?pid=1446 题目描述: One way that the police finds the head of a gang ...
- iOS 详细解释@property和@synthesize关键字
/** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...
- 解决 IE 不支持 document.getElementsByClassName() 的方法
//create method getElementsByClassName for document if(!document.getElementsByClassName){ document.g ...
- 腾讯视频嵌入手机端网站demo - 就像微信文章中一样一样的
页面中的调用如下: <iframe id="video_iframe" class="video_iframe" src="TenVideoPl ...
- ASP.NET MVC 搭建简单网站 --1.前端页面布局和基本样式实现
学技术这件事儿本来就是学习现有的东西,然后变成自己的,本文当然也是借鉴的别人的东西,写出来作为一个对知识的巩固. 1.网站用的是MVC模式,新建一个MVC项目,建立一个APP1Controller, ...
- php导出word(可包含图片)
为大家介绍一个 php 生成 导出word(可包含图片)的代码,有需要的朋友可以参考下. 之前介绍过php生成word的例子,只是不能包含图片与链接. 今天 为大家介绍一个 php 生成 导出word ...
- CentOS 6.X更新Python2.7.x版本 安装pip
在安装新版之前安装 先安装bz2.zlib,执行下列代码进行安装 yum install -y zlib-devel bzip2-devel xz-libs wget openssl openssl- ...