1741: [Usaco2005 nov]Asteroids 穿越小行星群

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 231  Solved: 166
[Submit][Status][Discuss]

Description

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot. This weapon is quite expensive, so she wishes to use it sparingly. Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.

贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个NxN的网格(1≤N≤500),在网格内有K个小行星(1≤K≤10000). 幸运地是贝茜有一个很强大的武器,一次可以消除所有在一行或一列中的小行星,这种武器很贵,所以她希望尽量地少用.给出所有的小行星的位置,算出贝茜最少需要多少次射击就能消除所有的小行星.
 

Input

* Line 1: Two integers N and K, separated by a single space.

* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.

    第1行:两个整数N和K,用一个空格隔开.
    第2行至K+1行:每一行有两个空格隔开的整数R,C(1≤R,C≤N),分别表示小行星所在的行和列.

Output

* Line 1: The integer representing the minimum number of times Bessie must shoot.

一个整数表示贝茜需要的最少射击次数,可以消除所有的小行星

Sample Input

3 4
1 1
1 3
2 2
3 2

INPUT DETAILS:

The following diagram represents the data, where "X" is an
asteroid and "." is empty space:
X.X
.X.
.X.

Sample Output

2

OUTPUT DETAILS:

Bessie may fire across row 1 to destroy the asteroids at (1,1) and
(1,3), and then she may fire down column 2 to destroy the asteroids
at (2,2) and (3,2).

HINT

 

Source

Gold

题解:看了半天,感觉还是网络流= =,而且貌似还是二分图

分别以横坐标与纵坐标建立点集,然后根据各点来连边建立二分图,然后二分图匹配或者网络流秒之

网络流:(这个程序其实还可以优化——因为我在A掉此题之后才想到不需要根据每一个点再在中间建立一个节点,直接二分图就可以搞定)

 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g,w:longint;
next,anti:point;
end;
var
i,j,k,l,m,n,s,t,ans:longint;
a:array[..] of point;
d,dv:array[..] of longint;
function min(x,y:longint):longint;
begin
if x<y then min:=x else min:=y;
end;
procedure add(x,y,z:longint);
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
new(p);p^.g:=x;p^.w:=;p^.next:=a[y];a[y]:=p;
a[x]^.anti:=a[y];a[y]^.anti:=a[x];
end;
function dfs(x,flow:longint):longint;
var p:point;k:longint;
begin
if x=t then exit(flow);
p:=a[x];dfs:=;
while p<>nil do
begin
if (p^.w<>) and (d[x]=(d[p^.g]+)) then
begin
k:=dfs(p^.g,min(flow-dfs,p^.w));
if p^.w<>maxlongint then dec(p^.w,k);
if p^.anti^.w<>maxlongint then inc(p^.anti^.w,k);
inc(dfs,k);
if dfs=flow then exit;
end;
p:=p^.next;
end;
if d[s]=n then exit;
dec(dv[d[x]]);
if dv[d[x]]= then d[s]:=n;
inc(d[x]);inc(dv[d[x]]);
end;
begin
readln(n,m);
for i:= to n*+m+ do a[i]:=nil;
for i:= to n do
begin
add(,i,);add(n+m+i,n*+m+,);
end;
for i:= to m do
begin
readln(j,k);
add(j,n+i,);add(n+i,n+m+k,);
end;
s:=;t:=n*+m+;
n:=n*+m+;
fillchar(d,sizeof(d),);
fillchar(dv,sizeof(dv),);
dv[]:=n;ans:=;
while d[s]<n do inc(ans,dfs(s,maxlongint));
writeln(ans);
readln;
end.

二分图:(话说二分图居然比优化的不到位的网络流满是什么鬼QAQ)

 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g:longint;
next:point;
end;
var
i,j,k,l,m,n,ans:longint;
a:array[..] of point;
c,f:array[..] of longint;
procedure add(x,y:longint);
var p:point;
begin
new(p);p^.g:=y;
p^.next:=a[x];a[x]:=p;
end;
function check(x:longint):boolean;
var p:point;
begin
p:=a[x];
while p<>nil do
begin
if f[p^.g]<>i then
begin
f[p^.g]:=i;
if c[p^.g]= then
begin
c[p^.g]:=x;
exit(true);
end
else if check(c[p^.g]) then
begin
c[p^.g]:=x;
exit(true);
end;
end;
p:=p^.next;
end;
exit(false);
end;
begin
readln(n,m);
for i:= to n do a[i]:=nil;
for i:= to m do
begin
readln(j,k);
add(j,k);
end;
ans:=;
fillchar(c,sizeof(c),);
fillchar(f,sizeof(f),);
for i:= to n do if check(i) then inc(ans);
writeln(ans);;
readln;
end.
04-29 06:00