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;
    }
  • 相关阅读:
    go基础1:Hello world与变量声明
    linux 根据端口号查看占用进程的pid
    攻防世界web新手练习区WP
    github下载慢解决方法
    玩转MSFconsole
    中国蚁剑AntSword安装
    burpsuite出现Payload set 1: Invalid number settings的解决办法
    在同一台电脑上同时安装Python2和Python3
    msf转移木马进程
    网站后台爆破工具:WebCrack
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617561.html
Copyright © 2011-2022 走看看