zoukankan      html  css  js  c++  java
  • Antenna Placement(匈牙利算法 ,最少路径覆盖)

    Antenna Placement
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6991   Accepted: 3466

    Description

    The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them. 
     
    Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered? 

    Input

    On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set ['*','o']. A '*'-character symbolises a point of interest, whereas a 'o'-character represents open space. 

    Output

    For each scenario, output the minimum number of antennas necessary to cover all '*'-entries in the scenario's matrix, on a row of its own.

    Sample Input

    2
    7 9
    ooo**oooo
    **oo*ooo*
    o*oo**o**
    ooooooooo
    *******oo
    o*o*oo*oo
    *******oo
    10 1
    *
    *
    *
    o
    *
    *
    *
    *
    *
    *
    

    Sample Output

    17
    5

    Source

    无向二分图的最小路径覆盖 = 顶点数 – 最大二分匹配数/2 ;
    详情,点这里
     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<queue>
     4 #include<string.h>
     5 using namespace std;
     6 bool map[500][500] ;
     7 int vis[500][500] ;
     8 int a[500][500] ;
     9 int girl [500] ;
    10 bool sta[500] ;
    11 int cnt ;
    12 int row , col ;
    13 char st[500] ;
    14 int move[][2] = {1 , 0 , 0 , 1 , -1 , 0 , 0 , -1} ;
    15 
    16 bool hungary (int x)
    17 {
    18     for (int i = 1 ; i <= cnt ; i++) {
    19         if (map[x][i] && sta[i] == false) {
    20             sta[i] = true ;
    21             if (girl[i] == 0 || hungary (girl[i])) {
    22                 girl[i] = x ;
    23                 return true ;
    24             }
    25         }
    26     }
    27     return false ;
    28 }
    29 
    30 int main ()
    31 {
    32    // freopen ("a.txt" , "r" , stdin) ;
    33     int T ;
    34     cin >> T ;
    35     while (T--) {
    36         scanf ("%d%d" , &row , &col) ;
    37         getchar () ;
    38         cnt = 0 ;
    39         memset (vis , -1 , sizeof(vis)) ;
    40         memset (map , 0 , sizeof(map)) ;
    41         memset (a , -1 , sizeof(a)) ;
    42         memset (girl , 0 , sizeof(girl)) ;
    43         for (int i = 0 ; i < row ; i++) {
    44             gets (st) ;
    45             for (int j = 0 ; j < col ; j++) {
    46                if (st[j] == '*') {
    47                     a[i + 1][j + 1] = 1 + cnt++;
    48                }
    49             }
    50         }
    51        /* for (int i = 1 ; i <= row ; i++) {
    52             for (int j = 1 ; j <= col ; j++) {
    53                 printf ("%d " , a[i][j]);
    54             }
    55             puts ("") ;
    56         }*/
    57         for (int i = 1 ; i <= row ; i++) {
    58             for (int j = 1 ; j <= col ; j++) {
    59                 if (a[i][j] != -1) {
    60                     for (int k = 0 ; k < 4 ; k++) {
    61                         int x = i + move[k][0] ;
    62                         int y = j + move[k][1] ;
    63                         if (a[x] [y] != -1)
    64                             map[ a[i][j] ] [ a[x][y] ] = 1 ;
    65                     }
    66                 }
    67             }
    68         }
    69        /* for (int i = 1 ; i <= cnt ; i++) {
    70             for (int j = 1 ; j <= cnt ; j++) {
    71                 printf ("%d " , map[i][j]) ;
    72             }
    73             puts ("") ;
    74         }*/
    75         int all = 0 ;
    76         for (int i = 1 ; i <= cnt ; i++) {
    77             memset (sta , 0 , sizeof(sta)) ;
    78             if (hungary (i))
    79                 all ++ ;
    80         }
    81       //  printf ("cnt = %d , all = %d
    " , cnt , all) ;
    82         printf ("%d
    " , cnt - all / 2) ;
    83     }
    84     return 0 ;
    85 }
    View Code
  • 相关阅读:
    Nhibernate对象转JSON
    C# Windows服务
    C# 接收http请求
    C# XML 基础解系
    C# XML 序列化与反序列化
    C# Newtonsoft.Json 应用
    C# 读取自定义XML
    对图片添加水印
    iText: 对pdf文件添加水印
    java对Office文件处理技术(介绍)
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4316657.html
Copyright © 2011-2022 走看看