zoukankan      html  css  js  c++  java
  • poj 3041Asteroids

    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


    二分匹配模板题。。求武器和敌人之间的最大匹配。
    View Code
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int N=505;
     7 int map[N][N],vis[N];
     8 int mx[N],my[N];
     9 int n,m;
    10 int dfs(int u)
    11 {
    12     int i;
    13     for(i=1;i<=n;i++)
    14     {
    15         if(!vis[i]&&map[u][i])
    16         {
    17             vis[i]=1;
    18             if(my[i]==-1||dfs(my[i]))
    19             {
    20                 my[i]=u;
    21                 mx[u]=i;
    22                 return 1;
    23             }
    24         }
    25     }
    26     return 0;
    27 }
    28 int hungary()
    29 {
    30     int i,cnt=0;
    31     for(i=1;i<=n;i++)
    32         if(mx[i]==-1)
    33         {
    34             memset(vis,0,sizeof(vis));
    35             if(dfs(i)) cnt++;
    36         }
    37     return cnt;
    38 }
    39 int main()
    40 {
    41     int i,j,u,v;
    42     scanf("%d%d",&n,&m);
    43     memset(map,0,sizeof(map));
    44     memset(mx,-1,sizeof(mx));
    45     memset(my,-1,sizeof(my));
    46     for(i=1;i<=m;i++)
    47     {
    48         scanf("%d%d",&u,&v);
    49         map[u][v]=1;
    50     }
    51     printf("%d\n",hungary());
    52     return 0;
    53 }
  • 相关阅读:
    《数据密集型应用系统设计》读书笔记
    每周总结
    每周总结
    每周总结
    《数据密集型应用系统设计》读书笔记
    每周总结
    《重构》读书笔记
    每周总结
    软件过程与管理知识回顾
    操作系统知识汇总5-6章
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/3008734.html
Copyright © 2011-2022 走看看