POJ3026:Borg Maze (最小生成树)

Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18644   Accepted: 5990

题目链接http://poj.org/problem?id=3026

Description:

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

题意:

从S点出发,目的是要到达所有外星人的位置。然后在起点或者有外星人的点,可以进行“分组”,也就是进行多条路径搜索。

最后的总代价定义为所有组行走的步数之和。比如这个组一开始走了5步,然后分为两组,各走三步,最终总代价为11步。

问的就是所有外星人位置被到达后的最小总代价为多少。

题解:

这个看似是搜索...也很像是搜索。如果不是在最小生成树专题里面,我估计也不会想到最小生成树。

其实这个题如果能够想到最小生成树就简单了,最终的目的转化为S以及所有的A都连通嘛,并且最小代价。

那么就直接先bfs求出两点间的最短距离,然后根据距离信息建图,跑最小生成树就行了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 105;
int t,n,m;
int num[N][N],d[N][N];
char mp[N][N];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
struct Edge{
    int u,v,w;
    bool operator < (const Edge &A)const{
        return w<A.w;
    }
}e[N*N<<1];
struct node{
    int x,y;
};
int f[N*N];
int tot,cnt;
int find(int x){
    return f[x]==x?f[x]:f[x]=find(f[x]);
}
int Kruskal(){
    int ans=0;
    for(int i=0;i<=105;i++) f[i]=i;
    for(int i=1;i<=tot;i++){
        int fx=find(e[i].u),fy=find(e[i].v);
        if(fx==fy) continue ;
        f[fx]=fy;
        ans+=e[i].w;
    }
    return ans ;
}
bool ok(int x,int y){
    return x>=1 && x<=n && y>=1 && y<=m && mp[x][y]!='#';
}
void bfs(int sx,int sy){
    queue <node> q;
    memset(d,INF,sizeof(d));d[sx][sy]=0;
    node now;
    now.x=sx;now.y=sy;
    q.push(now);
    while(!q.empty()){
        node cur = q.front();q.pop();
        for(int i=0;i<4;i++){
            int x=cur.x+dx[i],y=cur.y+dy[i];
            if(!ok(x,y)||d[x][y]<=d[cur.x][cur.y]+1) continue ;
            d[x][y]=d[cur.x][cur.y]+1;
            now.x=x;now.y=y;
            q.push(now);
            if(mp[x][y]=='A'||mp[x][y]=='S'){
                e[++tot].u=num[sx][sy];e[tot].v=num[x][y];e[tot].w=d[x][y];
            }
        }
    }
}
int main(){
    cin>>t;
    while(t--){
        scanf("%d%d",&m,&n);
        tot = cnt = 0;
        char c;
        while(1){
            scanf("%c",&c);
            if(c!=' ') break ;
        }
        int first=1;
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++){
            getchar();
            first=0;
            for(int j=1;j<=m;j++){
                scanf("%c",&mp[i][j]);
                if(mp[i][j]=='S'||mp[i][j]=='A') num[i][j]=++cnt;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(num[i][j]) bfs(i,j);
            }
        }
        sort(e+1,e+tot+1);
        int ans = Kruskal();
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/heyuhhh/p/10372174.html