• 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
  • 相关阅读:
    【原创】使用开源libimobiledevice盗取iphone信息
    【原创】Arduino制作Badusb实践
    【原创】Aduino小车玩法全记录
    【原创】Arduino入门基础知识总结
    【原创】Arduino、arm、树莓派与单片机
    【原创】PM3破解IC卡记录
    【转】反编译D-Link路由器固件程序并发现后门
    DDOS分布式拒绝服务
    XSS 初识
    针对企业级别渗透测试流程
  • 原文地址:https://www.cnblogs.com/LK1994/p/3246755.html
Copyright © 2020-2023  润新知