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;
    }
    
    

    思路

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

    匈牙利算法

  • 相关阅读:
    EMQ X v4.3 正式发布:性能大幅提升,更好用的多语言扩展
    全新 EMQ X Cloud 物联网云平台重磅发布
    MQTT 和 CoAP 在 EMQ X 世界的一次「约会」
    Python MQTT 异步框架 —— HBMQTT
    EMQ X 团队:连接数亿关键 IoT 设备|EMQ 2021 全球招聘季
    让每一个来自社区的声音被听见、有回响:EMQ X 开源项目正式采用 RFC 流程
    Kuiper 1.1.2 正式发布
    Xamarin.Forms iOS打包上传AppStore,奇妙的崩溃 EXC_CRASH (SIGABRT)
    大数据系列修炼-Scala课程01
    文本摘要简述
  • 原文地址:https://www.cnblogs.com/xxffxx/p/11973304.html
Copyright © 2011-2022 走看看