POJ #3041 asteroids 30,412 min. overlay maximum matching Hungarian algorithm

Source: Internet
Author: User

Description

Bessie wants to navigate she spaceship through a dangerous asteroid field in the shape of an n x N grid (1 <= N <= 5 ). The grid contains K asteroids (1 <= k <=), which is conveniently located at the lattice points of the G Rid.
Fortunately, Bessie have a powerful weapon that can vaporize all the asteroids on any given row or column of the grid with A single shot. This weapon was quite expensive, so she wishes to use it sparingly. Given the location of the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate a ll 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 must shoot.

Sample Input

3 41 11 32 23 2

Sample Output

2

Hint

INPUT DETAILS:
The following diagram represents the data, where "X" is a asteroid and "." is empty space:
x.x
. X.
. X.

OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (all) and (1,3), and then she could fire down column 2 to destroy T He asteroids at (2,2) and (3,2).

Ideas

The modeling of this problem is very classical, we consider each row as an X node, each column as a Y node, each target one edge, then destroy all targets and the least number of launches means to select the fewest points, so that any one edge at least one endpoint is selected. And this corresponds to the definition of the concept of the minimum coverage of the dichotomy graph, which is solved by the Hungarian algorithm, by the theorem of the minimum point coverage number of the binary graph = maximum matching number.

The AC code is as follows:

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>Const intMAXN =510;Const intMAXK =10010;intn,k;intLINE[MAXN][MAXN];BOOLUSED[MAXN];intNEXT[MAXN];BOOLFind (intx) { for(inti =1; I <= N; i++) {        if(Line[x][i] &&!Used[i]) {Used[i]=true; if(!next[i] | |find (Next[i])) {Next[i]=x; return true; }        }    }    return false;}intmatch () {intsum =0;  for(inti =1; I <= N; i++) {memset (used,0,sizeof(used)); if(Find (i)) sum++; }    returnsum;}intMainvoid) {    intu,v;  while(~SCANF ("%d%d", &n, &k) {memset (line,0,sizeof(line)); Memset (Next,0,sizeof(next));  while(k--) {scanf ("%d%d", &u, &v); LINE[U][V]=1; } printf ("%d\n", Match ()); }    return 0;}
View Code

  

POJ #3041 asteroids 30,412 min. overlay maximum matching Hungarian algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.