zoukankan      html  css  js  c++  java
  • poj 3041 Asteroids 题解

    Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20686   Accepted: 11239

    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

    [Submit]   [Go Back]   [Status]   [Discuss]

    ——————————————————-——————————我是分割线——————————————————————————————————————

    二分图最大匹配。

    一个小行星,要么清理该行,要么该列。

    所以也就是每个小行星对应的行列中至少选择一样来清理。

    下面建图,如果我们把每行看成集合一中的点,每列看成集合二中的点,一个小行星看成是其对应行列的连线,那么也就是说不能存在某一条连线两边的点都没有被选中的情况。

    这恰好就是二分图最小点集覆盖的要求。

    再利用二分图最大匹配的König定理:

      最小点覆盖数 = 最大匹配数

    因此本题自然转化为求 二分图的最大匹配问题

     1 /*
     2     Problem:
     3     OJ:
     4     User:
     5     Time:
     6     Memory:
     7     Length:
     8 */
     9 #include<iostream>
    10 #include<cstdio>
    11 #include<cstring>
    12 #include<cmath>
    13 #include<algorithm>
    14 #include<queue>
    15 #include<cstdlib>
    16 #include<iomanip>
    17 #include<cassert>
    18 #include<climits>
    19 #include<vector>
    20 #include<list>
    21 #include<map>
    22 #define maxn 501
    23 #define F(i,j,k) for(int i=j;i<=k;i++)
    24 #define M(a,b) memset(a,b,sizeof(a))
    25 #define FF(i,j,k) for(int i=j;i>=k;i--)
    26 #define inf 0x7fffffff
    27 #define maxm 2016
    28 #define mod 1000000007
    29 //#define LOCAL
    30 using namespace std;
    31 int read(){
    32     int x=0,f=1;char ch=getchar();
    33     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    34     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    35     return x*f;
    36 }
    37 int n,m,k;
    38 int g[maxn][maxn];
    39 int px[maxn],py[maxn];
    40 bool vis[maxn];
    41 inline int path(int u)
    42 {
    43     F(i,1,n){
    44         if(g[u][i]&&!vis[i])
    45         {
    46             vis[i]=true;
    47             if(py[i]==-1||path(py[i]))
    48             {
    49                 px[u]=i;
    50                 py[i]=u;
    51                 return 1;
    52             }
    53         }
    54     }
    55     return 0;
    56 }
    57 inline int solve()
    58 {
    59     int cnt=0;
    60     M(px,-1);M(py,-1);
    61     F(i,1,n){
    62         if(px[i]==-1){
    63             M(vis,false);
    64             cnt+=path(i);
    65         }
    66     }
    67     return cnt;
    68 }
    69 int main()
    70 {
    71     std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
    72     #ifdef LOCAL
    73     freopen("data.in","r",stdin);
    74     freopen("data.out","w",stdout);
    75     #endif
    76     cin>>n>>k;
    77     F(i,1,k){
    78         int x,y;
    79         cin>>x>>y;
    80         g[x][y]=1;
    81     }
    82     cout<<solve()<<endl;
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    45、linux shell命令,ldconfig
    47、linux shell,ln链接
    43、linux shell命令,chmod
    39、linux 进程管理
    46、linux shell命令,chkconfig
    40、linux shell常用函数mkdir,rmdir,mount
    26、linux 几个C函数,nanosleep,lstat,unlink
    38、linux shell常用函数,nice
    44、linux shell命令,ldd
    41、linux shell常用函数,lsof
  • 原文地址:https://www.cnblogs.com/SBSOI/p/5916297.html
Copyright © 2011-2022 走看看