poj 3026 Borg Maze

题目大意:s会分裂,然后从s到A的最小的步是多少。

解题思路:先bfs算出A或者S到任何点的距离,然后最求最小生成树,用dij或者prim都可以



#include
#include
#include
#include
#include
using namespace std;
const int maxn = 100+10;
char mp[maxn][maxn];
int cost[maxn][maxn];
int a[maxn][maxn];
int T[maxn][maxn];
int d[4][2]={0,1,0,-1,1,0,-1,0};
struct Point{
    int x,y;
    Point(){};
    Point(int zz,int yy){
        x=zz;
        y=yy;
    }
};
struct Node{
    int u,v;
    int cost;
}node[maxn*maxn];
bool cmd(Node a,Node b){
    return a.cost q;
    while(!q.empty()) q.pop();
    q.push(Point(x,y));
    T[x][y]=0;
    while(!q.empty()){
        Point p;
        p=q.front();
        q.pop();
        if(a[p.x][p.y]!=-1){
            cost[a[x][y]][a[p.x][p.y]]=T[p.x][p.y];
        }
        for(int i=0;i<4;i++){
            int dx=p.x+d[i][0];
            int dy=p.y+d[i][1];
            if(mp[dx][dy]=='#' || T[dx][dy]!=-1) continue;
            T[dx][dy]=T[p.x][p.y]+1;
            q.push(Point(dx,dy));
        }
    }
}




int dij(int tot){
    for(int i=0;i<=tot;i++) pa[i]=i;
        int c=0;
        for(int i=0;i0){
                    node[c].u=i;
                    node[c].v=j;
                    node[c++].cost=cost[i][j];
                }
            }
        }
        sort(node,node+c,cmd);

        int sum=0;
        for(int i=0;i



这个题输入的数据应该有坑。。。。坑了我几个小时,

gets(mp[0]);这样存缓冲就可以过

而getchar()就不能过。。。。。。。有毒啊






你可能感兴趣的:(poj,3026,Borg,Maze,poj,最短路练习)