bzoj3571
同样的最小乘积XXX模型,这里显然是二分图带权匹配
我不会写KM……于是写了个费用流,由于是稠密图,会退化到n^4
然后本地跑了56s,交上去过了………………一定是我电脑太慢……
改天写个KM吧
const inf=*;
type node=record
po,next,flow,cost:longint;
end;
point=record
x,y:longint;
end; var e:array[..] of node;
ma,pre,cur,p,d:array[..] of longint;
v:array[..] of boolean;
a,b,c:array[..,..] of longint;
q:array[..] of longint;
j,len,i,tt,t,n,ans:longint;
p1,p2:point; procedure add(x,y,f,c:longint);
begin
inc(len);
e[len].po:=y;
e[len].flow:=f;
e[len].cost:=c;
e[len].next:=p[x];
p[x]:=len;
end; procedure build(x,y,f,c:longint);
begin
add(x,y,f,c);
add(y,x,,-c);
end; function spfa:boolean;
var f,r,x,y,i,j:longint;
begin
fillchar(v,sizeof(v),false);
for i:= to t do
d[i]:=inf;
d[]:=;
f:=;
r:=;
q[]:=;
while f<=r do
begin
x:=q[f];
v[x]:=false;
i:=p[x];
while i<>- do
begin
y:=e[i].po;
if e[i].flow> then
if d[x]+e[i].cost<d[y] then
begin
d[y]:=d[x]+e[i].cost;
cur[y]:=i;
pre[y]:=x;
if not v[y] then
begin
inc(r);
q[r]:=y;
v[y]:=true;
end;
end;
i:=e[i].next;
end;
inc(f);
end;
if d[t]=inf then exit(false) else exit(true);
end; procedure change(j:longint);
begin
dec(e[j].flow);
inc(e[j xor ].flow);
end; function get:point;
var i,j,x,y:longint;
begin
len:=-;
fillchar(p,sizeof(p),);
for i:= to n do
begin
build(,i,,);
build(i+n,t,,);
end;
x:=; y:=;
for i:= to n do
for j:= to n do
begin
build(i,j+n,,c[i,j]);
if c[i,j]<c[x,y] then
begin
x:=i;
y:=j;
end;
end;
ma[y+n]:=x;
i:=p[x];
while i<>- do
begin
if (e[i].po=y+n) then change(i);
if e[i].po= then change(i xor );
i:=e[i].next;
end;
i:=p[y+n];
while i<>- do
begin
if e[i].po=t then
begin
change(i);
break;
end;
i:=e[i].next;
end;
while spfa do
begin
i:=t;
while i<> do
begin
j:=cur[i];
change(j);
if (i>n) and (i<>t) then
ma[i]:=pre[i];
i:=pre[i];
end;
end;
get.x:=; get.y:=;
for i:= to n do
begin
j:=ma[i+n];
// write(j,' ');
inc(get.x,a[j,i]);
inc(get.y,b[j,i]);
end;
// writeln;
if get.x*get.y<ans then ans:=get.x*get.y;
end; function cross(a,b,c:point):longint;
begin
exit((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x));
end; procedure work(p1,p2:point);
var p:point;
i,j:longint;
begin
for i:= to n do
for j:= to n do
c[i,j]:=a[i,j]*(p1.y-p2.y)+b[i,j]*(p2.x-p1.x);
p:=get;
if cross(p2,p,p1)>= then exit;
work(p1,p);
work(p,p2);
end; begin
readln(tt);
while tt> do
begin
dec(tt);
readln(n);
t:=*n+;
for i:= to n do
for j:= to n do
read(a[i,j]);
for i:= to n do
for j:= to n do
read(b[i,j]);
for i:= to n do
for j:= to n do
c[i,j]:=a[i,j];
ans:=*;
p1:=get;
for i:= to n do
for j:= to n do
c[i,j]:=b[i,j];
p2:=get;
work(p1,p2);
writeln(ans);
end;
end.
bzoj3571的更多相关文章
- bzoj3571————2016——3——12(最小乘积匹配)
bzoj3571 传送门http://www.lydsy.com/JudgeOnline/problem.php?id=3571 题解: ——————来自伟大的thy大神 http://blog.c ...
- BZOJ3571 [Hnoi2014]画框 【分治 + KM算法】
题目链接 BZOJ3571 题解 如果知道最小乘积生成树,那么这种双权值乘积最小就是裸题了 将两权值和作为坐标,转化为二维坐标系下凸包上的点,然后不断划分分治就好了 这里求的是最小匹配值,每次找点套一 ...
- BZOJ3571 : [Hnoi2014]画框
题目是要求最小乘积最小权匹配, 将一种方案看做一个二维点(x,y),x=a值的和,y=b值的和,所有方案中只有在下凸壳上的点才有可能成为最优解 首先要求出两端的方案l,r两个点 l就是a值的和最小的方 ...
- bzoj3571: [Hnoi2014]画框 最小乘积匹配+最小乘积XX总结,
思路大概同bzoj2395(传送门:http://www.cnblogs.com/DUXT/p/5739864.html),还是将每一种匹配方案的Σai看成x,Σbi看成y,然后将每种方案转化为平面上 ...
- BZOJ3571 & 洛谷3236:[HNOI2014]画框——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3571 https://www.luogu.org/problemnew/show/P3236 小T ...
- 【挖坑】thusc前一周计划2016.5.30-2016.6.3
首先开了徐姥爷blog&&AC记录里的几题,然后还有几个算法&&模板题要搞掉. 今天&&明天: bzoj3571/3083/2752/2727/2728 ...
- [暑假的bzoj刷水记录]
(这篇我就不信有网站来扣) 这个暑假打算刷刷题啥的 但是写博客好累啊 堆一起算了 隔一段更新一下. 7月27号之前刷的的就不写了 , 写的累 代码不贴了,可以找我要啊.. 2017.8.27upd ...
- 【bzoj3751】 Hnoi2014—画框
http://www.lydsy.com/JudgeOnline/problem.php?id=3571 (题目链接) 题意 给出一个$2*N$个点的二分图,$N*N$条边,连接$i$和$j$的边有两 ...
- HNOI2014
本蒟蒻到现在才把$HNOI2014$的坑填完... $AFO$之后码力急速下降... 感觉都没有码力了... 附上题解: $DAY1$: $T1$: BZOJ3571: [Hnoi2014]画框 $T ...
随机推荐
- 《C++Primer》复习——with C++11 [2]
1.数组引用形参,C++允许将变量定义成数组的引用,给予同样的道理,形参也可以是数组的引用,此时引用形参绑定到对应的实参上,也就是绑定到数组上 ]) { for (auto elem : arr) c ...
- 【BZOJ】【2132】圈地计划
网络流/最小割 Orz Hzwer 这类大概是最小割建模中的经典应用吧…… 黑白染色,然后反转黑色的技巧感觉很巧妙!这个转化太神奇了…… /****************************** ...
- 各种matrix
http://www.gamedev.net/topic/602722-worldviewproj/
- 关于去哪儿网的UI自动化测试脚本(Python实现)
UI自动化测试Qunar机票搜索场景访问Qunar机票首页http://flight.qunar.com,选择“单程”,输入出发.到达城市,选择today+7日后的日期,点“搜索”,跳转到机票单程搜索 ...
- jQuery经典面试题及答案精选[转载]
问题:jQuery的美元符号$有什么作用? 回答:其实美元符号$只是”jQuery”的别名,它是jQuery的选择器,如下代码: $(document).ready(function(){ }); 当 ...
- android 使用sqlite的一些注意事项
①在Activity里创建SQLiteOpenHelper对象时,不要在成员变量里面传入context参数,而要在onCreate里面创建这个SQLiteOpenHelper对象.因为如果在成员变量里 ...
- chmod命令详细用法
指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...
- [转]Java数组初始化详解
一维数组1) int[] a; //声明,没有初始化 2) int[] a=new int[5]; //初始化为默认值,int型为0 3) int[] a={1,2,3,4,5}; ...
- Project Euler 88:Product-sum numbers 积和数
Product-sum numbers A natural number, N, that can be written as the sum and product of a given set o ...
- 自旋锁spin_lock和raw_spin_lock
自旋锁spin_lock和raw_spin_lock Linux内核spin_lock.spin_lock_irq 和 spin_lock_irqsave 分析 http://blog.csdn.ne ...