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面向对象编程》
    大学计算机基础考试系统(CS)
    企业物资管理系统
    IP.21出现的错误
    假如你的年龄超过了23···
    如何经营婚姻
    一张舞女图测试你的左右脑切换能力【我看到左右都转呢~】
    无法加载DLL(oci.dll)
    mongodb安装信息及有关命令
    loaded the "controller" nib but the view outlet was not set.问题解决
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617561.html
Copyright © 2011-2022 走看看