zoukankan      html  css  js  c++  java
  • POJ 3041 Asteroids【二分图最大匹配】

    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
    思路:匈牙利算法,和最大流算法类似,二分图的最大匹配数等于最小点覆盖数

    #include<stdio.h>   
    #include<string.h>
    #include<iostream>
    using namespace std; 
    #define N 501
    int used[N];  
    int link1[N];     
    int map[N][N];     
    int n; 
    int DFS(int t)
    {
        int i;
        for(i=0; i<n; i++)  
        {
            if(!used[i]&&map[t][i])      
            {
                used[i]=1;
                if(link1[i]==-1||DFS(link1[i]))  
                {            
                    link1[i]=t;            
                    return 1;    
                }
            }
        }
        return 0;
    } 
    int main()
    {
        int T, i, nn, x, j, y, k;
        while(scanf("%d%d", &n, &k)!=EOF)
        { 
            memset(link1, -1, sizeof(link1));
            memset(map, 0, sizeof(map));
            for(i=0; i<k; i++)
            { 
                scanf("%d%d", &x, &y);
                map[x-1][y-1]=1;
            }
            int num=0; 
            for(i=0; i<n; i++)
            {
                memset(used, 0, sizeof(used));    
                if(DFS(i))    
                    num++;
            }
            printf("%d\n", num); 
        }
        return 0;
    }
  • 相关阅读:
    大数据在智慧城市中的应用
    使用Java+NetBeans设计web服务和页面,用Tomcat部署网页
    VS2013环境下配置OSG(3.4.0版本)
    sort函数用于vector向量的排序
    利用eigen库简单实现矩阵功能
    elasticsearch搜索QueryStringQueryBuilder时的一些问题记录
    Hbase中HMaster作用
    国内加速git下载速度
    JAVA中Integer.valueOf, parsetInt() String.valueOf的区别和结果
    JAVA API操作hbase1.4.2
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617561.html
Copyright © 2011-2022 走看看