zoukankan      html  css  js  c++  java
  • POJ 3041 Asteroids(二分图的最大匹配)

    Asteroids

    Time Limit: 1000MS

     

    Memory Limit: 65536K

    Total Submissions: 9171

     

    Accepted: 4921

    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

    Hint

    INPUT DETAILS: 
    The following diagram represents the data, where "X" is an asteroid and "." is empty space: 
    X.X 
    .X. 
    .X. 

    OUTPUT DETAILS: 
    Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).

    Source

    USACO 2005 November Gold

     解题报告:这是我第一个二分图的最大匹配的题,从昨天晚上就开始看有关的知识,还是有点看不懂,只是套用了模板,这个题和猎人打鸟一样那个题一样

    猎人要在n*n的格子里打鸟,他可以在某一行中打一枪,这样此行中的所有鸟都被打掉,也可以在某一列中打,这样此列中的所有鸟都打掉.问至少打几枪,才能打光所有的鸟?(猎人用的是无声枪);

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int N = 510;
    int map[N][N], visit[2 * N], match[2 * N];//match[i]记录与i相连的边
    int n, m, ans;
    int Find(int p)
    {
    int i;
    for(i = 1; i <= n; ++i)
    {
    if (!visit[i] && map[p][i])
    {
    visit[i] = 1;
    if (!match[i] || Find(match[i]))//寻找是否是增广路径,
    {
    match[i] = p;//路径取反操作
    return 1;
    }
    }
    }
    return 0;
    }
    int main()
    {
    int i, x, y;
    while (scanf("%d%d", &n, &m) != EOF)
    {
    memset(map, 0, sizeof(map));
    memset(match, 0, sizeof(match));
    for (i = 0; i < m; ++i)
    {
    scanf("%d%d", &x, &y);
    map[x][y] = 1;
    }
    ans = 0;
    for (i = 1; i <= n; ++i)
    {
    memset(visit, 0, sizeof(visit));
    if (Find(i))
    {
    ans ++;
    }
    }
    printf("%d\n", ans);
    }
    return 0;
    }



  • 相关阅读:
    JaveScript简单数据类型(JS知识点归纳二)
    JaveScript变量的简介及其变量的简单使用(JS知识点归纳一)
    JaveScript用二分法与普通遍历(冒泡)
    include、include_once、require、require_once其区别
    POST和GET有什么区别?
    前端向后台发送请求有几种方式?
    jQuery的$.ajax方法响应数据类型有哪几种?本质上原生ajax响应数据格式有哪几种,分别对应哪个属性?
    java根据汉字生成首字母大写
    springboot+使用切面AOP动态获取自定义注解
    JavaScript基础05——严格模式
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2389494.html
Copyright © 2011-2022 走看看