• poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)


    http://poj.org/problem?id=3041

    Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12601   Accepted: 6849

    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

     
    【题解】:
      最大匹配最小顶点覆盖
     
    【code】:
     1 /**
     2 Judge Status:Accepted     Memory:1880K    
     3 Time:16MS        Language:G++    
     4 Code Lenght:1182B   Author:cj
     5 */
     6 
     7 #include<iostream>
     8 #include<stdio.h>
     9 #include<string.h>
    10 #include<math.h>
    11 #include<algorithm>
    12 
    13 #define N 550
    14 using namespace std;
    15 
    16 int n;
    17 int map[N][N];
    18 int cx[N],cy[N],mark[N];
    19 
    20 int path(int u)
    21 {
    22     int j;
    23     for(j=1;j<=n;j++)
    24     {
    25         if(map[u][j]&&!mark[j])
    26         {
    27             mark[j]=1;
    28             if(cy[j]==-1||path(cy[j]))
    29             {
    30                 cx[u] = j;
    31                 cy[j] = u;
    32                 return 1;
    33             }
    34         }
    35     }
    36     return 0;
    37 }
    38 
    39 int maxMatch()
    40 {
    41     memset(cx,-1,sizeof(cx));
    42     memset(cy,-1,sizeof(cy));
    43     int i;
    44     int res = 0;
    45     for(i=1;i<=n;i++)
    46     {
    47         if(cx[i]==-1)
    48         {
    49             memset(mark,0,sizeof(mark));
    50             res+=path(i);
    51         }
    52     }
    53     return res;
    54 }
    55 
    56 int main()
    57 {
    58     int k,i;
    59     scanf("%d%d",&n,&k);
    60     memset(map,0,sizeof(map));
    61     for(i=1;i<=k;i++)
    62     {
    63         int r,c;
    64         scanf("%d%d",&r,&c);
    65         map[r][c]=1;
    66     }
    67     printf("%d
    ",maxMatch());
    68     return 0;
    69 }
  • 相关阅读:
    Msql-51CTO笔记
    Elasticsearch 学习第一天
    大数据开发参考资料
    1.docker的安装
    java_根据实体字段中的中文汉字排序
    遍历set集合,进行数据的拼接
    关于integer 和int
    03.linux环境安装mysql8的安装包
    02.linux下面安装jdk8
    01.VMware15.5下安装Centos7
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3267876.html
Copyright © 2020-2023  润新知