zoukankan      html  css  js  c++  java
  • poj 3041 Asteroids (匈牙利算法,最小覆盖数 = 最大匹配数)


    poj 3041 Asteroids

      Time Limit: 1 Sec
      Memory Limit: 64 MB

    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).

    题目地址:codevs 1022 覆盖

    题目大意: 题目很简洁了:)

    题解:

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


    AC代码

    #include <stdio.h>
    #include <cstring>
    using namespace std;
    const int N=505;
    int n,m,Ans;
    struct edge{
        int to,next;
    }e[10005];
    int cnt_edge,last[N];
    void add_edge(int u,int v){
        e[++cnt_edge]=(edge){v,last[u]};last[u]=cnt_edge;
    }
    int res[N];
    bool used[N];
    bool find(int u){
        for(int i=last[u];i;i=e[i].next){
            int v=e[i].to;
            if(!used[v]){
                used[v]=1;
                if(!res[v] || find(res[v])){
                    res[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            add_edge(x,y);
        }
        for(int i=1;i<=n;i++){
            memset(used,0,sizeof(used));
            if(find(i))Ans++;
        }
        printf("%d
    ",Ans);
        return 0;
    }
    


      作者:skl_win
      出处:https://www.cnblogs.com/shaokele/
      本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Struts2
    HIbernate缓存
    spring的静态代理和动态代理
    JVM与垃圾回收机制(GC)和类的生命周期
    java开发设计六大基本原则
    数据表链表结构
    HashMap的底层实现
    string与位运算
    log4j和logback
    C#深入类的方法
  • 原文地址:https://www.cnblogs.com/shaokele/p/9916226.html
Copyright © 2011-2022 走看看