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......)这几天开始会抽时间刷的,每天几道 ...
随机推荐
- SSH连接时出现「WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!」解决办法
用ssh來操控github,沒想到連線時,出現「WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!」,後面還有一大串英文,這時當然要向Google大神求助 ...
- 方法 :PHP开发环境搭建(phpstorm + xampp+mongodb)
phpstorm 安装下载 百度网盘资源 phpstorm 9.0.1(有序列号) http://pan.baidu.com/s/1kTvX0jl xampp 安装下载 ...
- ubuntu12.10设置禁止锁屏和屏幕常亮
1.System Settings -> Brightness and Lock -> Turn off screen... set to "Never" 进入ubun ...
- Swift 学习笔记1
最近在看Swift,努力在看相关的文档以及书籍,因为Swift3.0的更新,以及它开源了,所以打算写一些关于Swift的相关文章.让Swift能够更好的被我理解
- 如何使用 require.js ,实现js文件的异步加载,避免网页失去响应,管理模块之间的依赖性,便于代码的编写和维护。
一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...
- Django数据库配置
将Django使用数据库由默认的sqlite3更改为mysql: 1.安装mysql驱动程序 MySQLdb(mysql-python) mysqlclient Connector/Python Py ...
- TypeScript学习指南第一章--基础数据类型(Basic Types)
基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...
- java枚举类型enum的使用
2015-10-24 java达人 Java 中 的枚举类型采用关键字enum 来定义,从jdk1.5才有的新类型,所有的枚举类型都是继承自Enum 类型.要了解枚举类型,建议大家先打开jdk 中的E ...
- Spring核心框架 - AOP的原理及源码解析
一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...
- Mac OS系统 - 将视频转换成gif
github中开源轻量级应用:droptogif