zoukankan      html  css  js  c++  java
  • Asteroids(二分图最大匹配模板题)

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12323   Accepted: 6716

    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).
     1 #include<stdio.h>
     2 #include<string.h>
     3 int map[510][510];
     4 int link[510];
     5 int vis[510];
     6 int n;
     7 bool dfs(int u)
     8 {
     9     int v;
    10     for(v = 1; v <= n; v++)
    11     {
    12         if(map[u][v] && !vis[v])
    13         {
    14             vis[v] = 1;
    15             if(link[v] == -1 || dfs(link[v]))
    16             {
    17                 link[v] = u;
    18                 return true;
    19             }
    20         }
    21     }
    22     return false;
    23 }
    24 int main()
    25 {
    26 
    27     int k,u,v,res;
    28     memset(map,0,sizeof(map));
    29     scanf("%d %d",&n,&k);
    30     while(k--)
    31     {
    32         scanf("%d %d",&u,&v);
    33         map[u][v] = 1;
    34     }
    35     memset(link,-1,sizeof(link));
    36     res = 0;
    37     for(u = 1; u <= n; u++)
    38     {
    39         memset(vis,0,sizeof(vis));
    40         if(dfs(u)) res++;
    41     }
    42     printf("%d
    ",res);
    43     return 0;
    44 
    45 
    46 }
    View Code
  • 相关阅读:
    idea 开发中常用的36个快捷键!
    算法学习一
    elastic-job-lite使用文档
    Windows使用Fiddler对手机抓包或调试本地计算机web站点方法
    Thread.join的作用和原理
    static修饰的代码块被称作静态代码块
    【Java面试题】52 java中会存在内存泄漏吗,请简单描述。
    数据类型 Object.keys,values,entries
    数据类型 Map and Set(映射和集合)
    数组映射到对象
  • 原文地址:https://www.cnblogs.com/LK1994/p/3246755.html
Copyright © 2011-2022 走看看