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;
    }
  • 相关阅读:
    hbase 学习笔记一---基本概念
    hdu 4496 (并差集)
    rqnoj-105-核电站问题-dp
    面试之BI-SQL--table转换
    Android:从程序员到架构师之路Ⅲ_高焕堂
    Linux 的进程组、会话、守护进程
    Oracle创建dblink报错:ORA-01017、ORA-02063解决
    JSP 指令
    JSP 生命周期
    JSP 结构
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617561.html
Copyright © 2011-2022 走看看