码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3041 Asteroids (二分图最大匹配 == 最小点覆盖数)

时间:2015-08-27 18:51:09      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:acm   poj   算法   编程   二分匹配   


应该属于最基本的匹配问题,重点在于为什么可以把行和列化为二分图的左右两个集合,理解好长时间,可以尝试这样理解:一个炸弹只能炸掉一行 或着 一列,左右两个集合中的值分别代表某一行或着某一列,因为连线的意义是如果某一行某一列锁定的值有行星才连线,我们所要求的是最少的炸弹数即最少的行数和列数之和即选出最少的行数和列数从左右两个集合中,这些行和列满足的要求是能够覆盖所有的边,换句话说这些行和列的炸弹能够炸掉所有的行星。所以实质上就成了求最小的点集覆盖 == 最大匹配数。

/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-08-27 16:22
#
#     Filename: C.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/
#
#include<iostream>    
#include<sstream>    
#include<algorithm>    
#include<cstdio>    
#include<string.h>    
#include<cctype>    
#include<string>    
#include<cmath>    
#include<vector>    
#include<stack>    
#include<queue>    
#include<map>    
#include<set>    
using namespace std;
int N, K;
const int INF = 505;
int from[INF],tot;
bool use[INF];
vector<int>G[INF];
bool match(int x){
    for(int i = 0; i < G[x].size(); i++){
        if(!use[G[x][i]]){
            use[G[x][i]] = 1;
            if(from[G[x][i]] == -1 || match(from[G[x][i]])){
                from[G[x][i]] = x;
                return 1;
            }
        }
    }
   return 0;
}

int hungary(){
    tot = 0;
    memset(from, 255, sizeof(from));
    for(int i = 1; i <= N; i++){
        memset(use, 0, sizeof(use));
        if(match(i))
            ++tot;
    }
    return tot;
}

int main(){
    while(scanf("%d%d",&N, &K) != EOF){
      for(int i = 0; i < K; i++){
        int a, b;
        scanf("%d%d",&a, &b);
        G[a].push_back(b);
    }
    int ans = hungary();
    cout<<ans<<endl;
    for(int i = 0; i < INF; i++){
        G[i].clear();

    }
 }
    return 0;
}
/*
 3 4
 1 1
 1 3
 3 1
 2 2
 */


Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17878   Accepted: 9742

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

Source


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 3041 Asteroids (二分图最大匹配 == 最小点覆盖数)

标签:acm   poj   算法   编程   二分匹配   

原文地址:http://blog.csdn.net/lsgqjh/article/details/48029533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!