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

    思路

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

    匈牙利算法

  • 相关阅读:
    将迁移学习用于文本分类 《 Universal Language Model Fine-tuning for Text Classification》
    深度 | 提升深度学习模型的表现,你需要这20个技巧(附论文)
    [线性代数] 矩阵白化
    基于搜索的贝叶斯网络结构学习算法-K2
    Deep learning:四十三(用Hessian Free方法训练Deep Network)
    2020年AI、CV、NLP顶会最全时间表
    浅谈人脸识别中的loss 损失函数
    控制uniFrame显示的一个管理类
    php+sqlserver之如何操作sqlserver数据库
    php支持连接sqlserver数据库
  • 原文地址:https://www.cnblogs.com/xxffxx/p/11973304.html
Copyright © 2011-2022 走看看