在一个迷宫里面需要把一些字母。也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离,
**********************************************************************************
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; int  dir[][] = { {,},{,},{-,},{,-} };
char G[maxn][maxn];          //保存地图
int  D[maxn][maxn];          //记录两点间的距离
int  use[maxn][maxn];        //标记地图
int  Index[maxn][maxn];      //记录‘A’或者‘B’的编号
struct node{int x, y, step;}; void BFS(int k, int M,int N, int x, int y)
{
    queue<node> Q;
    node s;
    s.x = x, s.y = y, s.step = ;
    use[s.x][s.y] = k; Q.push(s);     while(Q.size())
    {
        s = Q.front();Q.pop();
        if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z')
            D[k][ Index[s.x][s.y] ] = s.step;         for(int i=; i<; i++)
        {
            node q = s;
            q.x += dir[i][], q.y += dir[i][];             if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && use[q.x][q.y]!=k)
            {
                use[q.x][q.y] = k;
                q.step += ;
                Q.push(q);
            }
        }
    }
}
int  Prim(int N)            //这里面的N代表编号最多到N
{
    int i, dist[maxn], vis[maxn]={, };
    int ans = , T=N-;     for(i=; i<=N; i++)
        dist[i] = D[][i];     while(T--)
    {
        int k=, mini = oo;         for(i=; i<=N; i++)
        {
            if(!vis[i] && mini > dist[i])
                mini = dist[i], k=i;
        }
        ans += mini;
        vis[k] = true;         for(i=; i<=N; i++)
            if(!vis[i])dist[i] = min(dist[i], D[k][i]);
    }     return ans;
} int main()
{
    int T;     scanf("%d", &T);     while(T--)
    {
        int i, j, M, N, t=;         scanf("%d%d ", &N, &M);         for(i=; i<M; i++)
        {
            gets(G[i]);
            for(j=; j<N; j++)
            {
                if(G[i][j]>='A' && G[i][j]<='Z')
                    Index[i][j] = t++;
                use[i][j] = ;
            }
        }         for(i=; i<M; i++)
        for(j=; j<=N; j++)
        {
            if(G[i][j]>='A' && G[i][j]<='Z')
                BFS(Index[i][j], M, N, i, j);
        }         int ans = Prim(t-);         printf("%d\n", ans);
    }     return ;
}

J - Borg Maze - poj 3026(BFS+prim)的更多相关文章

  1. POJ 3026(BFS+prim)

    http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...

  2. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  4. (最小生成树) Borg Maze -- POJ -- 3026

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  5. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  6. Borg Maze POJ - 3026 (BFS + 最小生成树)

    题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...

  7. Borg Maze poj 3026

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  8. poj3026(bfs+prim)

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

  9. poj3026(bfs+prim)最小生成树

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

随机推荐

  1. Apache MINA 框架之Handler介绍

    IoHandler 具备以下几个功能: sessionCreated sessionOpened sessionClosed sessionIdle exceptionCaught messageRe ...

  2. 异步tcp通信——APM.Server 消息推送服务的实现

    消息推送服务 服务器推送目前流行就是私信.发布/订阅等模式,基本上都是基于会话映射,消息对列等技术实现的:高性能.分布式可以如下解决:会话映射可采用redis cluster等技术实现,消息对列可使用 ...

  3. 3xian之所在

    最后一天,漫天飘起了雪花,假装欢送我离去. 这次WF之战不太顺利,早期的C题大概花了1秒钟构思,然而由于输出格式多了一个空格直到两个半小时才逃脱Wrong Answer的纠缠.还好lynncui在期间 ...

  4. java.lang.ClassCastException

    是指类型转换出错 当前者的域小于后者的时候出现 譬如说:前者A是子类的对象,而后者B是父类的对象 若使用A = B;就会抛出java.lang.ClassCastException List<C ...

  5. jquery get checkbox inside element(td).

    <td id="skill"><input name="skill" type="checkbox" value=&quo ...

  6. 自定义UITableViewCell时, 使用autoLayout, 无法很好的做到屏幕适配

    解决方法: 重写cell的setFrame方法即可 - (void)setFrame:(CGRect)frame { frame.size.width = self.window.frame.size ...

  7. 重温web服务器--细说Tomcat服务器

    从大学开始接触java web的开发时就开始使用tomcat部署web项目,对它的理解仅仅停留在"这是个开源免费的servlet容器"的阶段,后来也接触了一些tomcat的体系,原 ...

  8. SWT/RAP计算器

    /** *2.测试 */ public class NofTest extends AbstractEntryPoint {        Text text,text2;    RemoteObje ...

  9. hibernate_validator_09

    创建自己的约束规则 尽管Bean Validation API定义了一大堆标准的约束条件, 但是肯定还是有这些约束不能满足我们需求的时候, 在这种情况下, 你可以根据你的特定的校验需求来创建自己的约束 ...

  10. cocos2d-x 实现粒子飞行特效

    效果图 说明 实现效果: 按下鼠标并且移动, 所到之处产生光圈 光圈会以窗口中心为终点, 并且会偏移自身角度对准终点, 然后持续飞行, 直到终点. 附件 下载源码, 请猛击这里!