在一个迷宫里面需要把一些字母。也就是 ‘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. 016--JLE JNG(小于等于)

    一.指令格式 条件转移指令 JLE/JNG 格式: JLE/JNG 标号地址 功能: 小于等于/不大于  时转到标号地址 JNG    有符号 不大于         则跳转    //Jump if ...

  2. 使用<input>标签做了两个按钮, 按钮之间间距如何去掉

    遇到的问题: 使用<input>标签做了两个按钮, 按钮之间有个间距不知道怎么去掉. 如下图: 问题解决: <input>是内联块状元素(inline-block); 内联元素 ...

  3. 在CSS文件中引入其他CSS文件

    引入CSS的方法有两种,一种是@import,一种是link 一.@import url('地址');二.<link href="地址" rel="styleshe ...

  4. Excel.Application手册

    ----转载:http://blog.csdn.net/xxfigo/article/details/6618129 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明 ...

  5. (转)phpmyadmin操作技巧:如何在phpmyadmin里面复制mysql数据库?

    转之--http://blogunion.org/posts/copy-mysql-data-in-phpmyadmin.html 对于每一个站长而言,都会遇到要进行网站测试的时候.这个时候,往往需要 ...

  6. 探究Activity的各回调方法

    刚毕业那会儿,一心想朝着java web的方向进军,却岂料实习的时候阴差阳错地踏入了Android的大门,自此人生跌宕起伏.坎坎坷坷,在一家外企参与了几个需要越过GFW才能使用的有关于体育赛事的项目, ...

  7. 微信小应用vs progressive-web-apps

    https://developers.google.com/web/progressive-web-apps/

  8. MySql奇葩问题汇总

    当字段名与关键词重叠时,sql语句中用``将字段名括起来,就可解决报错的问题.

  9. PHP文件系统概述

    >> 本文固定链接: http://php.ncong.com/php_course/file/wenjianxitong.html >> 转载请注明: 恩聪php 2014年 ...

  10. Android之Activity生命周期简介

    概述 有图有真相,所以先上图: 上图是从Android官网截下的Activity的生命周期流程图,结构非常清晰,它描述了Activity在其生命周期中所有可能发生的情况以及发生的先后顺序,下面就将结合 ...