zoukankan      html  css  js  c++  java
  • POJ 3041.Asteroids-Hungary(匈牙利算法)

    Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23963   Accepted: 12989

    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).
     
     
    题意就是消灭星星,一个N*N的网格,网格上有星星,打一次枪可以消灭一行或者一列。
    问最少打多少次枪,消灭所有的星星。
     
     思路很神奇,可以用匈牙利算法写 。
    把行当成一个集合,列当成一个集合。
    求最小覆盖点,也就是求最大匹配。
    思路很好,当然也可以用其他的写,在学二分图,就用匈牙利啦。
     
    代码(直接改的hdu的2063,嘎嘎嘎):
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <iostream>
     6 #include <sstream>
     7 #include <algorithm>
     8 #include <string>
     9 #include <queue>
    10 #include <ctime>
    11 #include <vector>
    12 using namespace std;
    13 const int maxn= 25;
    14 const int maxm=500+10;
    15 const int inf = 0x3f3f3f3f;
    16 typedef long long ll;
    17 int n,m;
    18 int match[maxm];
    19 bool visited[maxm];
    20 bool mp[maxm][maxm];
    21 bool Find(int x){
    22     for(int i=1;i<=n;i++){
    23         if(mp[x][i]&&visited[i]==0){
    24             visited[i]=true;
    25             if(match[i]==0||Find(match[i])){
    26                 match[i]=x;
    27                 return true;
    28             }
    29         }
    30     }
    31     return false;
    32 }
    33 int main(){
    34     int x,y,num;
    35     while(~scanf("%d%d",&n,&m)){
    36         memset(mp,0,sizeof(mp));
    37         memset(match,0,sizeof(match));
    38         memset(visited,0,sizeof(visited));
    39         for(int i=0;i<m;i++){
    40             scanf("%d%d",&x,&y);
    41             mp[x][y]=true;
    42         }
    43         num=0;
    44         for(int i=1;i<=n;i++){
    45             memset(visited,0,sizeof(visited));
    46             if(Find(i))num++;
    47         }
    48         printf("%d
    ",num);
    49     }
    50     return 0;
    51 }

    溜啦溜啦,去写多校的二分图的题啦。

     
     
  • 相关阅读:
    最方便建立进程池,线程池的方法
    python并发编程之多线程
    Cpython解释器支持的进程与线程
    进程与线程理论基础
    爬虫关于高性能方面
    Python中 and,or 的计算规则
    存储库之——MongoDB
    解析库之——beautifulsoup
    破解极验滑动验证码
    爬取拉勾网示例
  • 原文地址:https://www.cnblogs.com/ZERO-/p/7806000.html
Copyright © 2011-2022 走看看