zoukankan      html  css  js  c++  java
  • 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
    

    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<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    template<typename T> void read(T &x)
    {
        char ch;
        x=0;
        ch=getchar();
        int f=1;
        for(; ch<'0'||ch>'9'; ch=getchar())
            if(ch=='-')
                f=-1;
        for(; ch>='0'&&ch<='9'; ch=getchar())
            x=(x<<1)+(x<<3)+(ch&15);
        x=x*f;
    }
    template<typename T> void print(T x)
    {
        if(x<0)
        {
            putchar('-'),x*=-1;
        }
        if(x>10)
            print(x/10);
        putchar(x%10+'0');
    }
    const int N=500+10;
    int n,k;
    int line[N][N],used[N],ne[N];
    bool Find(int x)
    {
        for(int i=1; i<=n; i++)
        {
            if(line[x][i]&&!used[i])
            {
                used[i]=1;
                if(ne[i]==0||Find(ne[i])){
                    ne[i]=x;
                return true;
                }
            }
        }
        return false;
    }
    int match()
    {
        int sum=0;
        for(int i=1; i<=n; i++)
        {
            memset(used,0,sizeof(used));
            if(Find(i))
                sum++;
        }
        return sum;
    }
    int main()
    {
    
        int u,v;
        read(n),read(k);
        while(k--)
        {
            read(u),read(v);
            line[u][v]=1;
        }
        cout<<match()<<endl;
    }
    
    

    思路

    最小顶点覆盖==二分图的最大匹配。

    匈牙利算法

  • 相关阅读:
    [zz] 从VMM中终止GUEST OS中运行进程
    [zz]Linux流量监控工具 – iftop (最全面的iftop教程)
    [zz]Ubuntu终端下Nethogs网络流量监控工具
    AtomicBoolean运用
    JDK中的URLConnection参数详解
    java读取文本文件数据
    tomcat时区设置
    java的upload
    java复习(set 、list、map)
    NIO学习笔记1
  • 原文地址:https://www.cnblogs.com/xxffxx/p/11973304.html
Copyright © 2011-2022 走看看