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
  • 相关阅读:
    cxVerticalGrid赋值是实时更新
    cxGrid类似pagecontrol的效果
    cxgrid属性说明,每次用的时候费时费力查找。
    做一个平均数,合计数的sql查询
    FDMemTable 提示操作速度尤其是循环
    手机号码验证
    取字符串长度
    DELPHI 获取本月 的第一天 和 最后一天
    SQL Server 断开某个数据库所有连接(还原的时候需要)转自用保留
    元宇宙六大核心技术
  • 原文地址:https://www.cnblogs.com/LK1994/p/3246755.html
Copyright © 2011-2022 走看看