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;
    }
  • 相关阅读:
    占满屏幕的宽高,当把textarea换成其他标签的时候,怎么才能编辑?
    鼠标点击文本框后,里面的文字就消失或全选中
    jquery中选取兄弟节点的方法
    文本出现省略号
    滚动条的样式
    省略号的样式。
    input的placeholder在ie9下不兼容的结局办法。
    [CF1097D] Makoto and a Blackboard
    [CF552C] Vanya and Scales
    [CF1353E] K-periodic Garland
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617561.html
Copyright © 2011-2022 走看看