poj 3041 Asteroids

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22347   Accepted: 12108

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

【分析】

题目大意:每一个炮能够消灭一整行或者一整列,要求花费最少的炮将所有的行星都消灭。(对啊,多么贪心的题目啊)

一开始认为是贪心,因为我觉得只要看每一个点所在的列或者是行哪一个所包含的行星最多就在那个多的行或者是列用一个大炮,然而我不能证明这个思想的正确性,并且我知道我

在做二分图的练习题QWQ。

果然有人和我一开始的想法一样,在discuss里找的反例。

1100
1010
1001
0000
你那样就会输出4,其实是3.

hehe.

然后我再考虑二分图,怎样构图呢?二分的条件是什么呢?然后....没有然后了,死活想不出来。

题解:匈牙利算法求最小点覆盖。

介绍什么是最小点覆盖(因为我不知道)

再利用二分图最大匹配的König定理:

最小点覆盖数 = 最大匹配数

(PS:最小点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖图的所有的边。)

关键是怎样构图,因为我开始nc的想让点二分。

正解是将矩阵看做是一个特殊的二分图,将行和列看做两个点集。就是将每一行看成一个结点,每一列看成一个结点,如果有行星那么就将这两个结点连起来。

就是求二分图的最大匹配。

【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define N 501
int map[N][N],match[N];
int cnt,n,m,x,y;
bool vis[N];

bool path(int x)
{    
    for(int i=1;i<=n;i++)
    {
        if(map[x][i]&&!vis[i])
        {
            vis[i]=1;
            if(!match[i]||path(match[i]))
            {
                match[i]=x;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d",&x,&y);
        map[x][y]=1;
    }
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(path(i))
        cnt++;
    }
    printf("%d",cnt);
    return 0;
}
原文地址:https://www.cnblogs.com/zzyh/p/6836584.html