• Borg Maze (BFS预处理+最小生成树)


    题目连接:https://vjudge.net/problem/POJ-3026

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.

    Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.

    Input

    On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character, a space `` '' stands for an open space, a hash mark ``#'' stands for an obstructing wall, the capital letter ``A'' stand for an alien, and the capital letter ``S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the ``S''. At most 100 aliens are present in the maze, and everyone is reachable.

    Output

    For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

    Sample Input

    2
    6 5
    ##### 
    #A#A##
    # # A#
    #S  ##
    ##### 
    7 7
    #####  
    #AAA###
    #    A#
    # S ###
    #     #
    #AAA###
    #####  
    

    Sample Output

    8
    11

    思路:

    从每个‘A’ ‘S’ 开始进行bfs,从而得到从当前点到其他每个点的最短路程。之后就是很常规的建树kruscal了。

      1 #include <math.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <iostream>
      5 #include <algorithm>
      6 #include <string>
      7 #include <string.h>
      8 #include <vector>
      9 #include <map>
     10 #include <stack>
     11 #include <set>
     12 #include <queue>
     13 
     14 
     15 #define LL long long
     16 #define INF 0x3f3f3f3f
     17 #define ls nod<<1
     18 #define rs (nod<<1)+1
     19 const int maxn = 2e5+7;
     20 const double eps = 1e-9;
     21 
     22 int n,m,ans,cnt,fa[5050];
     23 
     24 int row,col;
     25 int a[60][60],vis[60][60];
     26 int dirx[4] = {1, 0, -1, 0};
     27 int diry[4] = {0, 1, 0, -1};
     28 struct P {
     29     int x,y;
     30     int step;
     31 }st,ed;
     32 
     33 struct node{
     34     int u, v, w;
     35 }e[maxn];
     36 
     37 bool cmp(node a, node b)
     38 {
     39     return a.w < b.w;
     40 }
     41 
     42 int fid(int x)
     43 {
     44     return x == fa[x] ? x : fid(fa[x]);
     45 }
     46 
     47 void init(int n)
     48 {
     49     for(int i = 1; i <= n; i++) {
     50         fa[i] = i;
     51     }
     52     ans = 0;
     53     cnt = 0;
     54 }
     55 
     56 void kruskal()
     57 {
     58     std::sort(e+1, e+m+1, cmp);
     59     for(int i = 1; i <= m; i++) {
     60         int eu = fid(e[i].u);
     61         int ev = fid(e[i].v);
     62         if(eu == ev) {
     63             continue;
     64         }
     65         ans += e[i].w;
     66         fa[ev] = eu;
     67         if(++cnt == n-1) {
     68             break;
     69         }
     70     }
     71 }
     72 
     73 void bfs(int x,int y,int idx) {
     74     memset(vis,0, sizeof(vis));
     75     std::queue<P> q;
     76     st.x = x;
     77     st.y = y;
     78     st.step = 0;
     79     vis[x][y] = 1;
     80     q.push(st);
     81     while (!q.empty()) {
     82         st = q.front();
     83         q.pop();
     84         for (int i=0;i<4;i++) {
     85             ed.x = st.x + dirx[i];
     86             ed.y = st.y + diry[i];
     87             ed.step = st.step + 1;
     88             if (ed.x>=1 && ed.x<=row && ed.y>=1 && ed.y<=col && !vis[ed.x][ed.y] && a[ed.x][ed.y] != -1) {
     89                 vis[ed.x][ed.y] = 1;
     90                 q.push(ed);
     91                 if (a[ed.x][ed.y] > 0) {
     92                     e[m].u = idx;
     93                     e[m].v = a[ed.x][ed.y];
     94                     e[m++].w = ed.step;
     95                 }
     96             }
     97         }
     98     }
     99 }
    100 
    101 
    102 int main() {
    103     int T;
    104     scanf("%d",&T);
    105     char ch[100];
    106     while (T--) {
    107         scanf("%d%d",&col,&row);
    108         gets(ch);
    109         n = 1,m = 1;
    110         memset(a,0, sizeof(a));
    111         for (int i=1;i<=row;i++) {
    112             gets(ch);
    113             for (int j=0;j<col;j++) {
    114                 if (ch[j] == '#')
    115                     a[i][j+1] = -1;
    116                 else if (ch[j] == 'A' || ch[j] == 'S')
    117                     a[i][j+1] = n++;
    118                 else if (ch[j] == ' ')
    119                     a[i][j+1] = 0;
    120             }
    121         }
    122         for (int i=1;i<=row;i++) {
    123             for (int j=1;j<=col;j++) {
    124                 if (a[i][j] > 0)
    125                     bfs(i,j,a[i][j]);
    126             }
    127         }
    128         n--;
    129         m--;
    130         init(n);
    131         kruskal();
    132         printf("%d
    ",ans);
    133     }
    134     return 0;
    135 }
  • 相关阅读:
    多线程编程(6) 从 CreateThread 说起[续四]
    多线程编程(7) 从 CreateThread 说起[续五]
    我最喜欢的邮件客户端软件IncrediMail Xe
    [公告]博客园建立了“专家区”
    [新功能]文章档案
    Lucene.Net的语言处理包中Lucene.Net.Analysis.Cn的Bug
    [好消息]博客园与博文视点、第二书店合作推出“读书心得区”
    推荐一篇有关GC的文章
    [小技巧]在NTFS分区中复制文件的同时如何复制权限
    ShartPoin无法创建门户网站的问题
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11893016.html
Copyright © 2020-2023  润新知