Asteroids POJ - 3041
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.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
题解:把光束当作图的顶点,而把小行星当作连接对应光束的边。这样转换后,光束的攻击方案即对应一个顶点集合S,而要求攻击方案能够摧毁所有的小行星,
也就是图中的每条边都至少有一个属于S的端点。这样一来问题就转化为了求最小的满足上述要求的顶点集合S。
-----------------------摘自《挑战程序设计竞赛》
1 #include<vector> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 8 const int maxv=1010; 9 const int maxk=10005; 10 11 int n,k,V; 12 int match[maxv]; 13 bool vis[maxv]; 14 vector<int> G[maxv]; 15 16 void addedge(int u,int v){ 17 G[u].push_back(v); 18 G[v].push_back(u); 19 } 20 21 bool DFS(int v){ 22 vis[v]=true; 23 for(int i=0;i<G[v].size();i++){ 24 int u=G[v][i],w=match[u]; 25 if(w<0||!vis[w]&&DFS(w)){ 26 match[v]=u; 27 match[u]=v; 28 return true; 29 } 30 } 31 return false; 32 } 33 34 int B_match(){ 35 int ans=0; 36 memset(match,-1,sizeof(match)); 37 for(int v=1;v<=V;v++){ 38 if(match[v]<0){ 39 memset(vis,false,sizeof(vis)); 40 if(DFS(v)) ans++; 41 } 42 } 43 return ans; 44 } 45 46 int main() 47 { cin>>n>>k; 48 V=n*2; 49 for(int i=0;i<k;i++){ 50 int u,v; 51 scanf("%d%d",&u,&v); 52 addedge(u,n+v); 53 } 54 int ans=B_match(); 55 cout<<ans<<endl; 56 }

浙公网安备 33010602011771号