zoukankan      html  css  js  c++  java
  • POJ-3041 Asteroids(二分图最大匹配dfs求增广路)

    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).

    #include<stdio.h>///二分图最大匹配   最小点覆盖数=最大匹配数
    #include<string.h>///将行和列转化成点,每个点转化为连接两边的线,构成一个二分图,要求最小射击次数,即覆盖所有的边(被抽象成边的点),用最小的点(被抽象成点的行列)覆盖来实现
    int match[505];///
    bool edge[505][505],vis[505];///edge存图,表示在第i行j列是否有点存在,vis标记是否询问过某个点
    int n,k;
    bool KM(int x)///DFS求增广路
    {
        for(int i=1;i<=n;i++)///遍历能与该点配对的点
        {
            if(!vis[i]&&edge[x][i])
            {
                vis[i]=true;///标记访问点
                if(match[i]==0||KM(match[i]))
                {
                    match[i]=x;///更新配对关系
                    return true;
                }
            }
        }
        return false;///若遍历了所有点
    }
    int main()
    {
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            int x,y;
            memset(edge,false,sizeof(edge));
            memset(match,0,sizeof(match));
            for(int i=0;i<k;i++)
            {
                scanf("%d%d",&x,&y);
                edge[x][y]=true;///这里行和列是不同的,第一行配对第三列和第三行配对第一列是两个点,因此是无向图
            }
            int sum=0;
            for(int i=1;i<=n;i++)
            {
                memset(vis,false,sizeof(vis));
                if(KM(i))sum++;///增广路计数结果即是最大匹配数量
            }
            printf("%d
    ",sum);
        }
    }
    
  • 相关阅读:
    浅谈HTTPS协议和SSL、TLS之间的区别与关系
    ECC加密算法原理入门介绍
    用实例给新手讲解RSA加密算法
    ECC椭圆曲线详解(有具体实例)
    设置VMware随系统开机自动启动并引导虚拟机操作系统
    Windows自带的端口转发工具netsh使用方法
    JAVA中使用P和Q分量计算N和D进行RSA运算
    VirtualBox虚拟机和主机之间的通信
    centos 系统管理维护指南
    页面找不到js方法的原因,关于EasyUI
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135842.html
Copyright © 2011-2022 走看看