1. 程式人生 > >POJ3026:Borg Maze (最小生成樹)

POJ3026:Borg Maze (最小生成樹)

devel collect uid borg maze arc inpu front void input

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;
}

POJ3026:Borg Maze (最小生成樹)