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
题解:看了半天,感觉还是网络流= =,而且貌似还是二分图
分别以横坐标与纵坐标建立点集,然后根据各点来连边建立二分图,然后二分图匹配或者网络流秒之
网络流:(这个程序其实还可以优化——因为我在A掉此题之后才想到不需要根据每一个点再在中间建立一个节点,直接二分图就可以搞定)
1 /************************************************************** 2 Problem: 1741 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:20 ms 7 Memory:1036 kb 8 ****************************************************************/ 9 10 type 11 point=^node; 12 node=record 13 g,w:longint; 14 next,anti:point; 15 end; 16 var 17 i,j,k,l,m,n,s,t,ans:longint; 18 a:array[0..20000] of point; 19 d,dv:array[0..20000] of longint; 20 function min(x,y:longint):longint; 21 begin 22 if x<y then min:=x else min:=y; 23 end; 24 procedure add(x,y,z:longint); 25 var p:point; 26 begin 27 new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p; 28 new(p);p^.g:=x;p^.w:=0;p^.next:=a[y];a[y]:=p; 29 a[x]^.anti:=a[y];a[y]^.anti:=a[x]; 30 end; 31 function dfs(x,flow:longint):longint; 32 var p:point;k:longint; 33 begin 34 if x=t then exit(flow); 35 p:=a[x];dfs:=0; 36 while p<>nil do 37 begin 38 if (p^.w<>0) and (d[x]=(d[p^.g]+1)) then 39 begin 40 k:=dfs(p^.g,min(flow-dfs,p^.w)); 41 if p^.w<>maxlongint then dec(p^.w,k); 42 if p^.anti^.w<>maxlongint then inc(p^.anti^.w,k); 43 inc(dfs,k); 44 if dfs=flow then exit; 45 end; 46 p:=p^.next; 47 end; 48 if d[s]=n then exit; 49 dec(dv[d[x]]); 50 if dv[d[x]]=0 then d[s]:=n; 51 inc(d[x]);inc(dv[d[x]]); 52 end; 53 begin 54 readln(n,m); 55 for i:=0 to n*2+m+1 do a[i]:=nil; 56 for i:=1 to n do 57 begin 58 add(0,i,1);add(n+m+i,n*2+m+1,1); 59 end; 60 for i:=1 to m do 61 begin 62 readln(j,k); 63 add(j,n+i,1);add(n+i,n+m+k,1); 64 end; 65 s:=0;t:=n*2+m+1; 66 n:=n*2+m+2; 67 fillchar(d,sizeof(d),0); 68 fillchar(dv,sizeof(dv),0); 69 dv[0]:=n;ans:=0; 70 while d[s]<n do inc(ans,dfs(s,maxlongint)); 71 writeln(ans); 72 readln; 73 end.
二分图:(话说二分图居然比优化的不到位的网络流满是什么鬼QAQ)
1 /************************************************************** 2 Problem: 1741 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:44 ms 7 Memory:1460 kb 8 ****************************************************************/ 9 10 type 11 point=^node; 12 node=record 13 g:longint; 14 next:point; 15 end; 16 var 17 i,j,k,l,m,n,ans:longint; 18 a:array[0..100000] of point; 19 c,f:array[0..100000] of longint; 20 procedure add(x,y:longint); 21 var p:point; 22 begin 23 new(p);p^.g:=y; 24 p^.next:=a[x];a[x]:=p; 25 end; 26 function check(x:longint):boolean; 27 var p:point; 28 begin 29 p:=a[x]; 30 while p<>nil do 31 begin 32 if f[p^.g]<>i then 33 begin 34 f[p^.g]:=i; 35 if c[p^.g]=0 then 36 begin 37 c[p^.g]:=x; 38 exit(true); 39 end 40 else if check(c[p^.g]) then 41 begin 42 c[p^.g]:=x; 43 exit(true); 44 end; 45 end; 46 p:=p^.next; 47 end; 48 exit(false); 49 end; 50 begin 51 readln(n,m); 52 for i:=1 to n do a[i]:=nil; 53 for i:=1 to m do 54 begin 55 readln(j,k); 56 add(j,k); 57 end; 58 ans:=0; 59 fillchar(c,sizeof(c),0); 60 fillchar(f,sizeof(f),0); 61 for i:=1 to n do if check(i) then inc(ans); 62 writeln(ans);; 63 readln; 64 end.