【二分匹配入门专题1】G - Asteroids poj3041【最小顶点覆盖】

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题意:有一个n*n的矩阵,输入k行数,每行数是一个点的坐标,一次性可以去掉矩阵一行或者一列的~~那个单词没查,反正就是去掉这些坐标点,问你最少的操作次数思路:就是一个最小顶点覆盖的模板题,和杭电的2119一样。
#include<stdio.h>
#include<string.h>
#define N 550

int n,k;
int book[N],e[N][N],match[N];

int dfs(int u)
{
    int i;
    for(i = 1; i <= n; i ++)
    {
        if(!book[i]&&e[u][i])
        {
            book[i] = 1;
            if(!match[i]||dfs(match[i]))
            {
                match[i] = u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int t1,t2,sum,i,j;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        sum = 0;
        memset(e,0,sizeof(e));
        memset(match,0,sizeof(match));
        for(i = 1; i <= k; i ++)
        {
            scanf("%d%d",&t1,&t2);
            e[t1][t2] = 1;
        }
        for(i = 1; i <= n; i ++)
        {
            memset(book,0,sizeof(book));
            if(dfs(i))
                sum ++;
        }
        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-11-02 11:01:46

【二分匹配入门专题1】G - Asteroids poj3041【最小顶点覆盖】的相关文章

【二分匹配入门专题1】P - Ants poj2565【km----卡精度】

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself. Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants tra

HDU2063(二分匹配入门模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9322    Accepted Submission(s): 4108 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求

【二分图匹配入门专题1】I - Hiding Gold light oj 1152【二分图匹配】-------------------我是终于不那么水的水题分割线------------------------

You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cov

HDU 2063 过山车(二分匹配入门)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.net/dark_scope/article/details/8880547 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using nam

【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】

the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximu

【二分图匹配入门专题1】K - Going Home hdu1533【km匹配】

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, unt

【二分图匹配入门专题1】D - Matrix hdu2119【最小顶点覆盖】

Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column . Your task is to give out the minimum times of deleting all the '1' in the matrix. InputThere are several test case

【二分图匹配入门专题1】L - Card Game hdu 3722【km算法】

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of

【二分图匹配入门专题1】M - Cyclic Tour hdu1853【km算法--判断自重边】

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total len