zoukankan      html  css  js  c++  java
  • Asteroids POJ

    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*N的矩阵,一些星球位于一些点上,要求消除所有星球,每次操作可消除一行或一列的星球,求最少要几次。

    最小点覆盖定义:二分图中,选取最少的点数,使这些点和所有的边都有关联(把所有的边的覆盖),叫做最小点覆盖。

    结论:最小点覆盖数 = 最大匹配数

    思路:把横坐标作为集合X,纵坐标作为集合Y,对于点(x,y),由X中的x连向Y中的y。对于每条边,只要x和y中有一个被删除即可,明显的最小点覆盖模型。

    AC代码:

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<string.h>
     4 #include<algorithm>
     5 using namespace std;
     6 #define maxn 5555
     7 int vis[maxn];
     8 int match[maxn];
     9 int e[maxn][maxn];
    10 int n,m;
    11 int dfs(int u){  //匈牙利算法模板
    12     for(int i=1;i<=n;i++){
    13         if(vis[i]==0&&e[u][i]==1){
    14             vis[i]=1;
    15             if(match[i]==0||dfs(match[i])){
    16                 match[i]=u;
    17                 return 1;
    18             }
    19         }
    20     }
    21     return 0;
    22 }
    23 int main(){
    24     cin>>n>>m;
    25     for(int i=1;i<=m;i++){
    26         int x,y;
    27         cin>>x>>y;
    28         e[x][y]=1;
    29     }
    30     memset(match,0,sizeof(match));
    31     int ans=0;
    32     for(int i=1;i<=n;i++){
    33         memset(vis,0,sizeof(vis));
    34         if(dfs(i))
    35             ans++;
    36     }
    37     printf("%d
    ",ans);
    38     return 0;
    39 }
  • 相关阅读:
    使用memset()要注意
    UPC快速学习笔记之Collective Operations
    二叉搜索树(结点添加、后继搜索、前驱搜索、结点删除)
    十分愚蠢的错误记录(C++创建对象时new的问题)
    BFS和DFS分别用于有向图和无向图的一点心得和总结
    不要在if判断语句中同时声明变量,注意在if中同时赋值和判断的优先级
    C++内存泄漏姿势之——eof()和delete释放内存
    C++中"was not declared in this scope"问题记录;以及通过正则表达式提取文件中数字的方法
    论fork()函数的进阶使用
    页面的修改、添加,以及验证控件的常见应用
  • 原文地址:https://www.cnblogs.com/pengge666/p/11624358.html
Copyright © 2011-2022 走看看