poj 3041 Asteroids

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).
题意 贝西想在一个危险的小行星场中以N x N网格(1<=N<=500)的形式导航她的宇宙飞船。网格包含K个小行星(1<=K<=10000),它们方便地位于网格的晶格点处。 幸运的是,贝西有一个强大的武器,可以用一枪蒸发网格中任何一行或列中的所有小行星。这个武器相当昂贵,所以她希望节省使用。考虑到所有小行星在该领域的位置,找到贝西需要发射的最小射击次数,以消除所有小行星。
分析 最小覆盖点==二分图的最大匹配

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=505;
int map[maxn][maxn];
int vis[1001];
int book[1001];  ///book[i]记录与i相连的边。
int n,m;
bool dfs(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=1;
            if(book[i]==0||dfs(book[i]))    ///若i没有于别的边相连,或者与i相连的那条边于别的边相连
            {
                book[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int a,b,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(map,0,sizeof(map));
        memset(book,0,sizeof(book));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            map[a][b]=1;
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值