zoukankan      html  css  js  c++  java
  • POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

    题目链接:

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

    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,使用匈牙利算法,计算二分最大匹配,最后求的最小顶点覆盖。
    AC代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 int n,k,e[510][510],pred[510],queue[250000],cx[510],cy[510];
     4 int maxmatch();
     5 int main()
     6 {
     7     int i,x,y;
     8     while(scanf("%d%d",&n,&k) != EOF)
     9     {
    10         memset(e,0,sizeof(e));
    11         for(i=1;i<=k;i++)
    12         {
    13             scanf("%d%d",&x,&y);
    14             e[x][y]=1;
    15         }
    16         printf("%d
    ",maxmatch());//输出最小顶点覆盖数 
    17     }
    18     return 0;
    19 }
    20 int maxmatch()
    21 {
    22     int i,j,y;
    23     int cur,tail,res=0;
    24     memset(cx,0xff,sizeof(cx));
    25     memset(cy,0xff,sizeof(cy));
    26     
    27     for(i=1;i<=n;i++)
    28     {
    29         if(cx[i] != -1)//找到x集合中每个未盖点i进行一次找交错轨 
    30         continue;
    31         
    32         for(j=1;j<=n;j++)
    33             pred[j]=-2;//初始化为-2
    34              
    35         cur=0;//队列初始化 
    36         tail=0;
    37         
    38         for(j=1;j<=n;j++)//将i的邻接顶点加入队列 
    39         {
    40             if(e[i][j])
    41             {
    42                 pred[j]=-1;//-1表示遍历到,是邻接顶点 
    43                 queue[tail++]=j;
    44             }
    45         }
    46         
    47         while(cur < tail)//BFS 
    48         {
    49             y=queue[cur];
    50             if(cy[y]==-1)
    51             break;//找到了一个未匹配的点,则找到了一条交错轨 
    52             cur++;
    53             //已经匹配给cy[y]了,从cy[y]出发,将其邻接点加入队列 
    54             for(j=1;j<=n;j++)
    55             {
    56                 if(pred[j] == -2 && e[ cy[y ]][j])
    57                 {
    58                     pred[j]=y;
    59                     queue[tail++]=j;
    60                 }
    61             }
    62         }
    63         if(cur == tail)//没有找到交错轨 
    64         continue; 
    65         
    66         while(pred[y] > -1)//更改交错轨上的匹配状态 
    67         {
    68             cx[ cy[pred[y]] ] = y;
    69             cy[y]=cy[ pred[y] ];
    70             y=pred[y];
    71         }
    72         cy[y]=i;
    73         cx[i]=y;
    74         
    75         res++;//匹配数加1 
    76     }
    77     return res;
    78 }
  • 相关阅读:
    mybatis cache标签的参数
    debian ab压力测试环境
    Linux/debian 服务器buff/cache占用过多 清除cache脚本
    IKAnalyzerUtil中文分词
    JVM调优
    debian9 rc.local开机启动文件不存在解决方案 并开机启动tomcat容器
    spring mvc-获取 @resposeBody
    SerializeUtil 序列化工具
    JS_4DOM
    Spring_5
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7363159.html
Copyright © 2011-2022 走看看