Description
Bessie wants to navigate she spaceship through a dangerous asteroid field inthe shape of an n x N grid (1 <= N <= 50 0). The grid contains K asteroids (1 <= k <=), which is conveniently located at the lattice pointsof the grid .
Fortunately, Bessie have a powerful weapon that can vaporize all the Asteroidsin any given row or column of the grid with a Single shot. This weapon was quiteexpensive, so she wishes to use it sparingly. Given the location of any theasteroids in the field, find the minimum number of shots Bessie needs to fireto eliminate all of the asteroids.
Input
* Line 1:two integers N and K, separated to a single space.
* Lines 2..k+1:each line contains, 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 Mustshoot.
Sample Input
3 4
1 1
1 3
2 2
3 2
Sample Output
2
INPUT DETAILS:
The following diagram represents the data, where "X" is a Asteroidand "." is empty space:
x.x
. X.
. X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at () and (1,3), andthen She-fire-down-column 2 to destroy th E asteroids at (2,2) and (3,2).
Topic Analysis: The target point of the horizontal and vertical axis as two sets, then a goal is an edge, a gun can be eliminated with the point adjacent to all the edges, so a minimum point of a graph can be covered, that is, the maximum number of binary graph matching.
Use the Hungarian algorithm to find the augmented path. With DFS implementations, initialization is a bit of an open point. Search each point in turn, if there is an augmented path, the number of matches plus 1;
AC Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace Std;
const int maxn=505;
Vector<int> G[MAXN]; adjacency Table Storage Diagram
int n,m;
BOOL vis[505];//Mark Point
int link[maxn];//matching relationship, initialized to-1
BOOL Dfs (int s) {
for (int i=0;i<g[s].size (); ++i) {
int u=g[s][i];
if (Vis[u]) continue;
Vis[u]=true;
if (Link[u]==-1 | | dfs (LINK[U)) {//u not covered, or U's matching adjacency not covered dot
Link[u]=s;
return true;
}
}
return false;
}
int hungry () {
int ans=0;
memset (link) (link,-1,sizeof);//Initialize each point not covered
for (int i=1;i<=n;++i) {
memset (vis,false,sizeof (VIS));
if (Dfs (i)) ans++;//has an augmented path, and each augmentation path is longer than the number of matches
}
return ans;
}
int main ()
{
scanf ("%d%d", &n,&m);
for (int i=1;i<=n;++i)
G[i].clear ();
for (int i=0;i<m;++i) {
int x, y;
scanf ("%d%d", &x,&y);
G[x].push_back (y);
}
printf ("%d\n", Hungry ());
return 0;
}
Eoj 2069 Asteroids binary graph maximum matching