BZOJ1693: [Usaco2007 Demo]Asteroids

来源:互联网 发布:电子实验室软件 编辑:程序博客网 时间:2024/06/16 11:22

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

INPUT DETAILS:

The following diagram represents the data, where “X” is an

asteroid and “.” is empty space:

X.X

.X.

.X.

Sample Output

2

题目传送门
题意:一个矩阵地图N x N 个格子(1 <= N <= 500)。
有K个地雷藏在格子里(1 <= K <= 10,000)。
有一种大炮,一击就可以让在同一水平或垂直的地雷清除。
求最少击炮次数才可以清除所有地雷。

二分图最小覆盖模版

They say memories make us who we are,that the past defines us.But we can't forget to grow,evolve,because sometimes,a memory can be so powerful that we get stuck in it,frozen in a moment. 有人说是记忆成就了现在的我们,过去造就了我们,但是我们不能忘记去成长、去进步。因为有时记忆可以强大到让我们故步自封,沉睡在一瞬间。                                   ——《Forever》

代码如下:

#include<cstdio>#include<cstring>using namespace std;int match[11000];bool chw[11000];struct node{    int x,y,next;}a[210000];int len,last[410000];void ins(int x,int y){    len++;    a[len].x=x;a[len].y=y;    a[len].next=last[x];last[x]=len;}int n,m,k;bool findmuniu(int x){    for(int k=last[x];k;k=a[k].next)    {        int y=a[k].y;        if(chw[y]==true)        {            chw[y]=false;            if(match[y]==0||findmuniu(match[y])==true)            {                match[y]=x;                return true;            }        }    }    return false;}int main(){    scanf("%d%d",&n,&m);    len=0;memset(last,0,sizeof(last));    for(int i=1;i<=m;i++)    {        int x,y;        scanf("%d%d",&x,&y);        ins(x,y);    }    int ans=0;    memset(match,0,sizeof(match));    for(int i=1;i<=n;i++)    {        memset(chw,true,sizeof(chw));        if(findmuniu(i)==true)ans++;    }    printf("%d\n",ans);    return 0;}

by_lmy

阅读全文
0 0
原创粉丝点击