您的位置:首页 > 其它

邦德_纪中1236_最大权匹配_状压dp

2016-07-11 14:25 363 查看

Description

每个人都知道詹姆斯邦德,著名的007,但很少有人知道很多任务都不是他亲自完成的,而是由他的堂弟们吉米邦德完成(他有很多堂弟),詹姆斯已经厌倦了把一个个任务分配给一个个吉米,他向你求助。

每个月,詹姆斯都会收到一些任务,根据他以前执行任务的经验,他计算出了每个吉米完成每个任务的成功率,要求每个任务必须分配给不同的人去完成,每个人只能完成一个任务。

请你编写程序找到一个分配方案使得所有任务都成功完成的概率。

Input

输入第一行包含一个整数N,表示吉米邦德的数量以及任务的数量(正好相等,1<=N<=20)。

接下来N行,每行包含N个0到100之间整数,第i行的第j个数Aij表示吉米邦德i完成任务j成功的概率为Aij%

Output

输出所有任务成功完成最大的概率,结果保留6位小数。

Sample Input

输入1:

2

100 100

50 50

输入2:

2

0 50

50 0

输入3:

3

25 60 100

13 0 50

12 70 90

Sample Output

输出1:

50.000000

输出2:

25.000000

输出3:

9.100000

Data Constraint

n<=20

题解

看到题目一拍大腿确认是最大权匹配,然后就水到了90分

KM算法求最大权匹配,然后就是乘积。

正解就是状压dp

将n个任务选或不选的状态压缩成21位二进制数,然后转移

方程:

f[i]=max(f[j]∗a[j,i])

表示状态为i时的方案数可以从状态j转移

限制:j的1的个数 < i的1的个数

又一次体验MLE和不看数据范围的快♂感,回味无穷

水解/pas:

var
map:array[1..30,1..30] of longint;
link,lx,ly,ls:array[1..300] of longint;
x,y:array[1..3100] of boolean;
n:longint;
procedure init;
var
i,j,w:longint;
begin
readln(n);
for i:=1 to n do
for j:=1 to n do read(map[i,j]);
end;
function find(v:longint):boolean;
var
i,k:longint;
begin
find:=true;
x[v]:=true;
for i:=1 to n do
if not y[i] and(lx[v]+ly[i]=map[v,i]) then
begin
y[i]:=true;
k:=link[i];
link[i]:=v;
if (k=0)or find(k) then exit;
link[i]:=k;
end;
find:=false;
end;
procedure main;
var
i,j,k,d:longint;
begin
fillchar(lx,sizeof(lx),0);
fillchar(ly,sizeof(ly),0);
for i:=1 to n do
for j:=1 to n do
if map[i,j]>lx[i] then
lx[i]:=map[i,j];
for k:=1 to n do
repeat
fillchar(x,sizeof(x),0);
fillchar(y,sizeof(y),0);
if find(k) then break;
d:=maxint;
for i:=1 to n do
if x[i] then
for j:=1 to n do
if not y[j] then
if lx[i]+ly[j]-map[i,j]<d then
d:=lx[i]+ly[j]-map[i,j];
for i:=1 to n do if x[i] then lx[i]:=lx[i]-d;
for i:=1 to n do if y[i] then ly[i]:=ly[i]+d;
until 1=2;
end;
procedure print;
var
i:longint;
ans:real;
begin
ans:=100;
for i:=1 to n do
ans:=ans*map[link[i],i]/100;
writeln(ans:0:6);
end;
begin
init;
main;
print;
end.


dp解/pas:

var
n,i,j:longint;
q:array[0..1500000]of longint;
a:array[1..32,1..32]of real;
f:array[0..1500000]of real;
t:array[0..32]of longint;
function max(x,y:real):real;
begin
max:=x;
if y>x then
max:=y;
end;
begin
readln(n);
for i:=1 to n do
for j:=1 to n do
begin
read(a[i,j]);
a[i,j]:=a[i,j]/100;
end;
q[1]:=1;
for i:=2 to n do q[i]:=q[i-1]*2;
f[0]:=1;
for i:=1 to q
*2-1 do
begin
t[0]:=0;
for j:=1 to n do
if q[j]and i<>0 then
begin
inc(t[0]);
t[t[0]]:=j;
end;
for j:=1 to t[0] do
f[i]:=max(f[i],f[i-q[t[j]]]*a[t[0],t[j]]);
end;
writeln(f[q
*2-1]*100:0:6);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: