zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    centos 安装docker
    vsphere client 创建虚拟机 如何关联到本地iso文件
    在centos7中使用supermin制作centos6.5docker镜像
    centos安装后,连接不上网络,yum命令执行can not find a valid baseurl for repo: base/7/x86-64
    佛祖保佑,永无BUG
    框架原理
    android 中解析XML的方法(转)
    android 项目随记一
    android事件系列-onTouch事件与手势操作
    深入理解 AngularJS 的 Scope(转)
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3267876.html
Copyright © 2011-2022 走看看