POJ 3014:asteroids (binary matching, Hungarian algorithm)

Source: Internet
Author: User

Asteroids
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14399 Accepted: 7836

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).

All right.. This type of simple question is first brushed to this. To deepen the investment.


#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace    std;const int M = + 5;int N, m;int link[m];bool map[m][m];bool cover[m];int ans;void init () {int num;    int x;    int y;    memset (map, False, sizeof (map));            for (int i=1; i<=m; i++) {scanf ("%d%d", &x, &y);    Map[x][y]=true; }}bool dfs (int x) {for (int y=1; y<=n; y++) {if (Map[x][y] &&!cover[y]) {cover[            Y]=true;                if (Link[y]==-1 | | dfs (LINK[Y])) {link[y]=x;            return true; }}} return false;}        int main () {while (scanf ("%d%d", &n, &m)!=eof) {ans=0;        Init ();        memset (link, 1, sizeof);            for (int i=1; i<=n; i++) {memset (cover, False, sizeof (cover));        if (Dfs (i)) ans++;    } printf ("%d\n", ans); } return 0;}




POJ 3014:asteroids (binary 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.