您的位置:首页 > 其它

pku2226 Muddy Fields

2012-03-04 18:19 232 查看
最小覆盖问题,跟3041差不多,只不过有了不能覆盖点的限制,首先横竖染色,那么每个水坑要么没横着的覆盖,要么被竖着的覆盖,用染出色的该点木板颜色连边,求最大匹配即可。

View Code

program pku2226(input,output);
var
map     : array[0..51,0..51] of char;
x,y     : array[0..51,0..51] of longint;
f     : array[0..2500,0..2500] of boolean;
v     : array[0..2500] of boolean;
lk     : array[0..2500] of longint;
n,m     : longint;
ll,rr : longint;
procedure init;
var
i,j : longint;
begin
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do
read(map[i,j]);
readln;
end;
fillchar(x,sizeof(x),255);
fillchar(y,sizeof(y),255);
end; { init }
procedure make_graph();
var
i,j : longint;
tot : longint;
begin
tot:=0;
for i:=1 to n do
for j:=1 to m do
begin
if map[i,j]='.' then
continue
else
begin
if x[i,j-1]<>-1 then
x[i,j]:=x[i,j-1]
else
begin
inc(tot);
x[i,j]:=tot;
end;
end;
end;
ll:=tot+1;

for i:=1 to m do
for j:=1 to n do
begin
if map[j,i]='.' then
continue
else
begin
if y[j-1,i]<>-1 then
y[j,i]:=y[j-1,i]
else
begin
inc(tot);
y[j,i]:=tot;
end;
end;
end;
rr:=tot;

fillchar(f,sizeof(f),false);
for i:=1 to n do
for j:=1 to m do
if map[i,j]='*' then
f[x[i,j],y[i,j]]:=true;
end; { make_graph }
function find(now :longint ):boolean;
var
i : longint;
begin
for i:=ll to rr do
if (not v[i])and(f[now,i]) then
begin
v[i]:=true;
if (lk[i]=0)or(find(lk[i])) then
begin
lk[i]:=now;
exit(true);
end;
end;
exit(false);
end; { find }
function main:longint;
var
i : longint;
begin
main:=0;
fillchar(lk,sizeof(lk),0);
for i:=1 to ll-1 do
begin
fillchar(v,sizeof(v),false);
if find(i) then
inc(main);
end;
end; { main }
begin
while not eof do
begin
init;
make_graph;
writeln(main);
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: