Asteroids
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24789 | Accepted: 13439 |
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.
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.
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.
* 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.
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
Sample Output
2
Hint
INPUT DETAILS:
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
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).
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
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).
题意
一个n*n的网格,每个网格中有一些点,每次可以消灭一行或者一列的点,求最少的次数消灭所有的点。
分析
这是一个二分图,行和列分别是两个集合。求最少点覆盖集。
结论:最小点覆盖集=二分图最大匹配数
建图跑最大流即可。
建图:S向行连流量为1的边,列向T连流量为1的边,对于每个点(u,v),由u向v连流量为1的边
code
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 5 using namespace std; 6 const int N = 1010; 7 const int INF = 1e9; 8 struct Edge{ 9 int to,nxt,c; 10 Edge() {} 11 Edge(int x,int y,int z) {to = x,c = y,nxt = z;} 12 }e[1000100]; 13 int q[1000100],L,R,S,T,tot = 1; 14 int dis[N],cur[N],head[N]; 15 16 inline char nc() { 17 static char buf[100000],*p1 = buf,*p2 = buf; 18 return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++; 19 } 20 inline int read() { 21 int x = 0,f = 1;char ch=nc(); 22 for (; ch<‘0‘||ch>‘9‘; ch=nc()) if(ch==‘-‘)f=-1; 23 for (; ch>=‘0‘&&ch<=‘9‘; ch=nc()) x=x*10+ch-‘0‘; 24 return x*f; 25 } 26 void add_edge(int u,int v,int c) { 27 e[++tot] = Edge(v,c,head[u]);head[u] = tot; 28 e[++tot] = Edge(u,0,head[v]);head[v] = tot; 29 } 30 bool bfs() { 31 for (int i=1; i<=T; ++i) cur[i] = head[i],dis[i] = -1; 32 L = 1,R = 0; 33 q[++R] = S;dis[S] = 1; 34 while (L <= R) { 35 int u = q[L++]; 36 for (int i=head[u]; i; i=e[i].nxt) { 37 int v = e[i].to; 38 if (dis[v] == -1 && e[i].c > 0) { 39 dis[v] = dis[u]+1;q[++R] = v; 40 if (v==T) return true; 41 } 42 } 43 } 44 return false; 45 } 46 int dfs(int u,int flow) { 47 if (u==T) return flow; 48 int used = 0; 49 for (int &i=cur[u]; i; i=e[i].nxt) { 50 int v = e[i].to; 51 if (dis[v] == dis[u] + 1 && e[i].c > 0) { 52 int tmp = dfs(v,min(flow-used,e[i].c)); 53 if (tmp > 0) { 54 e[i].c -= tmp;e[i^1].c += tmp; 55 used += tmp; 56 if (used == flow) break; 57 } 58 } 59 } 60 if (used != flow) dis[u] = -1; 61 return used; 62 } 63 int dinic() { 64 int ret = 0; 65 while (bfs()) ret += dfs(S,INF); 66 return ret; 67 } 68 69 int main() { 70 int n = read(),m = read(); 71 S = n + n + 1,T = n + n + 2; 72 for (int i=1; i<=n; ++i) add_edge(S,i,1),add_edge(i+n,T,1); 73 for (int i=1; i<=m; ++i) { 74 int u = read(),v = read(); 75 add_edge(u,v+n,1); 76 } 77 int ans = dinic(); 78 printf("%d",ans); 79 return 0; 80 }