Poj 3041 Asteroids

Source: Internet
Author: User

Asteroids
Time limit:1000 ms   Memory limit:65536 K
Total submissions:14371   Accepted:7822

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.

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.

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 dimo-represents the data, where "X" is an asteroid and "." Is empty space:
X. x
. X.
. X. 

Output details:
Bessie may fire your SS Row 1 to destroy the asteroids at () and (), and then she may fire down column 2 to destroy the asteroids at () and ).

Hungarian algorithm !!!!

It is mainly to understand how to transform the problem.

Convert the rows and columns into two sets. Then, coordinate points (I, j) are converted into J columns of the I row that can be matched. Finally, the minimum coverage point is found, which is defined by the Hungary theorem:Minimum coverage point = maximum matching edge.


If you still don't understand it, click here to open it.


The AC code is as follows:

#include<iostream>#include<cstring>using namespace std;int v[505],s[505],map[505][505];int n,m;int xyl(int a){    int i;    for(i=1;i<=n;i++)    {        if(!v[i]&&map[a][i])        {            v[i]=1;            if(!s[i]||xyl(s[i]))            {                s[i]=a;                return 1;            }        }    }    return 0;}int main(){    int i,j;    int a,b;    while(cin>>n>>m)    {        memset(s,0,sizeof s);        for(i=1;i<=m;i++)        {            cin>>a>>b;            map[a][b]=1;        }        int sum=0;        for(i=1;i<=n;i++)        {            memset(v,0,sizeof v);            if(xyl(i))                sum++;        }        cout<<sum<<endl;    }    return 0;}










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.