poj3041-asteroids-Hungarian algorithm

Source: Internet
Author: User

Asteroids
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23963 Accepted: 12989

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). Test instructions is to destroy the stars, a n*n grid, a grid of stars, a gun can destroy one row or a column.  Ask at least how many times to hit the gun and Destroy all the stars. The idea is magical and can be written in the Hungarian algorithm. Make a collection as a set. Find the minimum coverage point, that is, the maximum match. The idea is very good, of course, can also use other writing, learning two-point map, the use of Hungary. Code, directly change the hdu of the 2063, quack.
1#include <stdio.h>2#include <math.h>3#include <string.h>4#include <stdlib.h>5#include <iostream>6#include <sstream>7#include <algorithm>8#include <string>9#include <queue>Ten#include <ctime> One#include <vector> A using namespacestd; - Const intmaxn= -; - Const intmaxm= -+Ten; the Const intINF =0x3f3f3f3f; -typedefLong Longll; - intn,m; - intMATCH[MAXM]; + BOOLVISITED[MAXM]; - BOOLMP[MAXM][MAXM]; + BOOLFind (intx) { A      for(intI=1; i<=n;i++){ at         if(mp[x][i]&&visited[i]==0){ -visited[i]=true; -             if(match[i]==0||Find (Match[i])) { -match[i]=x; -                 return true; -             } in         } -     } to     return false; + } - intMain () { the     intX,y,num; *      while(~SCANF ("%d%d",&n,&m)) { $Memset (MP,0,sizeof(MP));Panax Notoginsengmemset (Match,0,sizeof(Match)); -Memset (visited,0,sizeof(visited)); the          for(intI=0; i<m;i++){ +scanf"%d%d",&x,&y); Amp[x][y]=true; the         } +num=0; -          for(intI=1; i<=n;i++){ $Memset (visited,0,sizeof(visited)); $             if(Find (i)) num++; -         } -printf"%d\n", num); the     } -     return 0;Wuyi}

Slip away, go and write a multi-school two-figure problem.

poj3041-asteroids-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.