(模板题)poj 3041 Asteroids(二分图的最大匹配匈牙利算法)

Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20143   Accepted: 10929

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 4
1 1
1 3
2 2
3 2

Sample Output

2

Hint

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

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

Source

USACO 2005 November Gold

提示

题意:
Bessie的飞船在n×n的网格形状小行星危险领域(1≤N≤500),该网格包含k个小行星(1 < = K = 10000),为了方便,我们把小行星看做在点上。
幸运的是,Bessie有一个强力的武器,可以消灭一行或一列所有的小行星,这种武器是相当昂贵的,所以她希望有节制地使用它。鉴于这一领域所有小行星的位置,找到消除所有的小行星最小使用武器次数。
思路:
二分图的最大匹配入门。
与最大匹配的相关问题:
1.最小路径覆盖;
2.拆点;
3.最小点集覆盖;
4.二分图的最大独立。
想要了解 更多戳我

示例程序

Source Code

Problem: 3041		Code Length: 784B
Memory: 1368K		Time: 0MS
Language: GCC		Result: Accepted
#include 
#include 
int map[500][500],v[500],match[500];
int dfs(int t,int n)
{
    int i;
    for(i=0;n>i;i++)
    {
        if(map[t][i]==1&&v[i]==0)
        {
            v[i]=1;
            if(match[i]==-1||dfs(match[i],n)==1)
            {
                match[i]=t;
                return 1;
            }
        }
    }
    return 0;
}
int pro(int n)
{
    int i,sum=0;
    memset(match,-1,sizeof(match));
    for(i=0;n>i;i++)
    {
        memset(v,0,sizeof(v));
        sum=sum+dfs(i,n);
    }
    return sum;
}
int main()
{
    int n,k,r,c,i,x,y;
    memset(map,0,sizeof(map));
    scanf("%d %d",&n,&k);
    for(i=1;k>=i;i++)
    {
        scanf("%d %d",&x,&y);
        x--;
        y--;
        map[x][y]=1;
    }
    printf("%d",pro(n));
    return 0;
}

你可能感兴趣的:(POJ,二分图,---Primary)