题目链接

题目大意:

任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小。

分析:

第一感觉使3维的BFS。但写着写着,发现不对。

应当用最小生成树解法。把每个字母(即A,或S)看成一个结点,如果求出来任意两个结点间的权值,则求解即为求最小生成树。

通过暴力,对每一个字母进行BFS,求出任意两个结点的距离。然后prim.

AC代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue> using namespace std; const int maxn = ;
const int INF = (<<); int dx[] = {, , -, };
int dy[] = {, -, , }; struct Pos {
int x, y;
}; char G[maxn][maxn]; //存放地图
bool vis[maxn][maxn]; //深搜时用的标记数组
int num[maxn][maxn], node[][]; //num表示字母编号(从0开始),node字母间的最短距离
int dis[maxn][maxn]; //深搜时用来记录到(sx,sy)的最短距离
int n, m, cn; void BFS(int sx, int sy) {
queue<Pos> Q; memset(vis, , sizeof(vis)); Q.push((Pos){sx, sy});
vis[sx][sy] = true;
dis[sx][sy] = ; while(!Q.empty()) {
Pos e = Q.front(); Q.pop();
int x = e.x, y = e.y; if(num[x][y] != -) //(x,y)这点是字母
node[num[sx][sy]][num[x][y]] = dis[x][y]; for(int d=; d<; d++) {
int nx = x+dx[d];
int ny = y+dy[d]; if(nx < && ny < && nx >= n && ny >= m) continue;
if(vis[nx][ny] || G[nx][ny] == '#') continue; vis[nx][ny] = true;
dis[nx][ny] = dis[x][y]+;
Q.push((Pos){nx, ny});
}
}
} int prim(int s) {
/*
* cn为结点数,node为邻接矩阵
* 任意两点间距离node[i][j]
* 本题就是prim模板
*/ int d[], ans = ;
bool v[]; memset(v, false, sizeof(v)); for(int i=; i<cn; i++) {
d[i] = node[s][i];
} v[s] = true;
d[s] = ; for(int i=; i<cn-; i++) {
int x, m=INF;
for(int y=; y<cn; y++) if(!v[y] && m >= d[y]) m = d[x=y];
v[x] = true;
ans += m;
for(int y=; y<cn; y++) if(!v[y] && d[y] > node[x][y]) d[y] = node[x][y];
} return ans;
} int main() {
int T, n, m, ns;
char tmp[]; scanf("%d", &T); while(T--) {
scanf("%d%d", &m, &n);
gets(tmp); //discuss上说后面有多个空格
for(int i=; i<n; i++) {
gets(G[i]);
} cn = ; //字母的个数
memset(num, -, sizeof(num)); for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(G[i][j] == 'S' || G[i][j] == 'A') { //对字母进行编号
num[i][j] = cn++;
} if(G[i][j] == 'S') { //'S'的编号
ns = cn-;
}
}
} for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(G[i][j] == 'A' || G[i][j] == 'S')
BFS(i, j);
}
} printf("%d\n", prim(ns));
} return ;
}

POJ3026 Borg Maze(最小生成树)的更多相关文章

  1. POJ3026:Borg Maze (最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18644   Accepted: 5990 题目链接:h ...

  2. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

  3. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  4. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14165   Accepted: 4619 Descri ...

  5. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  6. POJ3026 Borg Maze(Prim)(BFS)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 Descri ...

  7. POJ3026 Borg Maze(bfs求边+最小生成树)

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

  8. (POJ 3026) Borg Maze 最小生成树+bfs

    题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...

  9. poj 3026 Borg Maze (最小生成树+bfs)

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

随机推荐

  1. Qt 学习之路 :可视化显示数据库数据

    前面我们用了两个章节介绍了 Qt 提供的两种操作数据库的方法.显然,使用QSqlQuery的方式更灵活,功能更强大,而使用QSqlTableModel则更简单,更方便与 model/view 结合使用 ...

  2. Zlib文件压缩和解压

    开源代码:http://www.zlib.net/zlib使用手册:http://www.zlib.net/manual.htmlzlib wince版:http://www.tenik.co.jp/ ...

  3. Core Foundation框架介绍

    Core Foundation框架介绍 **参考网址: ARC下OC对象和CF对象之间的桥接 Core Foundation框架介绍 Core Foundation框架 Core Foundation ...

  4. 第一篇:Power BI数据可视化概述

    前言 "可视化之工具,可爱者甚蕃.统计学家独爱R,自Python来,世人盛爱matplotlib.余独爱Power BI之出微软而不染(免费),濯Office而不妖(够精简).......& ...

  5. 第四篇:R语言数据可视化之折线图、堆积图、堆积面积图

    折线图简介 折线图通常用来对两个连续变量的依存关系进行可视化,其中横轴很多时候是时间轴. 但横轴也不一定是连续型变量,可以是有序的离散型变量. 绘制基本折线图 本例选用如下测试数据集: 绘制方法是首先 ...

  6. linux 一些笔记内容

    #which COMMAND : 显示命令路径#whatis COMMAND : 命令出现在哪个章节#type COMMAND :显示一个命令是内部命令还是外部命令#printenv :显示系统信息 ...

  7. vim 设定一个新的snippets

    目录 ~/.vim/bundle/vim-snippets/snippets 编辑 _.snippets 在最后添加一行自己需要的,比如想要个自己的名字,然后按tab,即出现全称,可以添加如下内容 s ...

  8. 【iOS UISearchBar父控件是UIScrollView时,上移的问题】

    如果UISearchViewController的父控件是UIScrollView,点击UISearchBar后,它会移出控制器外.如下,使用UIScrollView作为"消息"和 ...

  9. 给方法传递参数:ref参数和out参数

    /*--------------------------------------------------- 给方法传递参数:ref参数和out参数 (P106) ------------------- ...

  10. php 写model层

    <?php /** * @author Administrator * */ class User { private $id; private $admin; private $paw; pr ...