首页 > 代码库 > poj3026Borg Maze(bfs预处理+最小生成树)

poj3026Borg Maze(bfs预处理+最小生成树)

题目链接:

啊哈哈,点我点我

思路:

首先把图中的A S预处理出来,然后对这些点逐一做bfs找到这些点到其它点的最短路径,然后建图完毕也用最小生成树的prim算法或者kruscal算法求出连接所有点的最短距离。。不知道为嘛用dis数组去维护为什么会超时,而在结构体里面用step数组却可以过,我也不知道为什么,纠结了很多天。。我把错误的代码贴出来,希望各位帮我找出原因,不胜感激。。。

题目:
Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8481 Accepted: 2852

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

Source

Svenskt M?sterskap i Programmering/Norgesmesterskapet 2001


tle代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=60*60;
int root[maxn],dis[maxn][maxn],map[maxn][maxn],d[maxn];
int gra[maxn][maxn];
bool vis[maxn][maxn],hash[maxn];

int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int n,m,t,cal;

struct Point
{
    int x,y;
}point[maxn];


bool check(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&map[x][y]>=0)
        return true;
    return false;
}

void prim()
{
    memset(hash,false,sizeof(hash));
    int now,tmp,ans=0;
    memset(d,INF,sizeof(d));
    d[1]=0;
    for(int i=1;i<=cal;i++)
    {
        tmp=INF;
        for(int j=1;j<=cal;j++)
        {
            if(!hash[j]&&tmp>d[j])
            {
                tmp=d[j];
                now=j;
            }
        }
        ans=ans+tmp;
        hash[now]=true;
        for(int j=1;j<=cal;j++)
        {
            if(!hash[j]&&d[j]>gra[now][j])
                d[j]=gra[now][j];
        }
    }
    cout<<ans<<endl;
}

void bfs(int sx,int sy)
{
    memset(dis,INF,sizeof(dis));
    memset(vis,false,sizeof(vis));
    queue<Point>Q;
    while(!Q.empty())  Q.pop();
    Point current,next;
    current.x=sx;
    current.y=sy;
    vis[sx][sy]=true;
    dis[sx][sy]=0;
    Q.push(current);
    while(!Q.empty())
    {
        current=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=current.x+dx[i];
            next.y=current.y+dy[i];
            if(check(next.x,next.y)&&dis[next.x][next.y]>dis[current.x][current.y]+1)
            {
                dis[next.x][next.y]=dis[current.x][current.y]+1;
                if(map[next.x][next.y]>=1)
                {
                    gra[map[next.x][next.y]][map[sx][sy]]=dis[next.x][next.y];
                    gra[map[sx][sy]][map[next.x][next.y]]=dis[next.x][next.y];
                }
                vis[next.x][next.y]=true;
                Q.push(next);
            }
        }
    }
}


void solve()
{
     for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         if(map[i][j]>0)
             bfs(i,j);
     prim();
}

int main()
{
    scanf("%d",&t);
    char str[maxn];
    while(t--)
    {
        cal=1;
        scanf("%d%d",&m,&n);
        gets(str);
        for(int i=1;i<=n;i++)
        {
            gets(str+1);
            for(int j=1;j<=m;j++)
            {
               if(str[j]=='#')  map[i][j]=-1;
               if(str[j]=='S')  map[i][j]=1;
               if(str[j]=='A')  map[i][j]=++cal;
               if(str[j]==' ')  map[i][j]=0;
            }
        }
        solve();
    }
    return 0;
}


ac代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=2500+10;
int dis[maxn][maxn],map[maxn][maxn],d[maxn],gra[maxn][maxn];
bool vis[maxn][maxn],Hash[maxn];

int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int n,m,t,cal;

struct Point
{
    int x,y,time;
}point[maxn];

int prim()
{
    memset(Hash,false,sizeof(Hash));
    int now,tmp,ans=0,cur;
    for (int i=2;i<=cal;i++) d[i]=INF;
    d[1]=0;
    for(int i=1;i<=cal;i++)
    {
        tmp=INF;
        for(int j=1;j<=cal;j++)
        {
            if(!Hash[j]&&tmp>d[j])
            {
                tmp=d[j];
                now=j;
            }
        }
        ans=ans+tmp;
        Hash[now]=true;
        for(int j=1;j<=cal;j++)
        {
            if(!Hash[j]&&d[j]>gra[now][j])
                d[j]=gra[now][j];
        }
    }
    return ans;
}

bool check(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&map[x][y]>=0)
        return true;
    return false;
}

void bfs(int sx,int sy)
{
    queue<Point>Q;
    while(!Q.empty())  Q.pop();
    memset(vis,false,sizeof(vis));
    Point current,next;
    current.x=sx;
    current.y=sy;
    current.time=0;
    vis[sx][sy]=true;
    Q.push(current);
    while(!Q.empty())
    {
        current=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=current.x+dx[i];
            next.y=current.y+dy[i];
            if(check(next.x,next.y))
            {
                next.time=current.time+1;
                if(map[next.x][next.y]>=1)
                {
                    gra[map[next.x][next.y]][map[sx][sy]]=next.time;
                    gra[map[sx][sy]][map[next.x][next.y]]=next.time;
                }
                vis[next.x][next.y]=true;
                Q.push(next);
            }
        }   
    }
}

void solve()
{
     for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
           if(map[i][j]>0)
              bfs(i,j);
     cout<<prim()<<endl;
}

int main()
{
    scanf("%d",&t);
    char str[maxn];
    while(t--)
    {
        cal=1;
        scanf("%d%d",&m,&n);
        gets(str+1);
        for(int i=1;i<=n;i++)
        {
            gets(str+1);
            for(int j=1;j<=m;j++)
            {
               if(str[j]=='#')  map[i][j]=-1;
               else if(str[j]=='S')  map[i][j]=1;
               else if(str[j]=='A')  map[i][j]=++cal;
               else if(str[j]==' ')  map[i][j]=0;
            }
        }
        solve();
    }
    return 0;
}