题目链接

题目大意:

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

分析:

第一感觉使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. [Unity3d][NGUI]两种思路解决AssetBundle的依赖关系.

    接上文. 使用上文中的AssetBundle打包方式生成的文件包括了依赖关系中的文件. 一般的使用中并不会发现什么问题. 可是当配合NGUI的时候,使用dynamicFont时打包AssetBundl ...

  2. [ES6] Array.find()

    Convenient method to find one item in an array, avoid writing and  for + if: let arys = [1,,5,,6] ; ...

  3. Weibo SSO认证 和初次请求数据

    在进行SSO请求之前 我们要先去新浪微博的开放平台http://open.weibo.com/进行创建应用.以便得到appKey 和AppSecret. 点击创建应用 .进行资料填写  在这里 App ...

  4. 学习《Spring 3.x 企业应用开发实战》Day-1

    Day-1 记录自己学习spring的笔记 提要:根据<Spring 3.x 企业应用开发实战>开头一个用户登录的例子,按照上面敲的. 1.项目分层

  5. Servlet与JSP的关系

    Servlet与JSP的异同点: 相似点: 都可以生成动态网页 不同点: JSP擅长网页制作,生成页面直观,但不易追踪与排错 Servlet是纯Java,擅长处理流程与业务逻辑,缺点是页面不直观

  6. 《Android开发艺术探索》读书笔记 (11) 第11章 Android的线程和线程池

    第11章 Android的线程和线程池 11.1 主线程和子线程 (1)在Java中默认情况下一个进程只有一个线程,也就是主线程,其他线程都是子线程,也叫工作线程.Android中的主线程主要处理和界 ...

  7. POJ 3449 Geometric Shapes (求正方形的另外两点)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1470   Accepted: 622 D ...

  8. JS遍历对象或者数组

    一.纯js实现 <script> var obj = {"player_id":"GS001","event_id":" ...

  9. WPF实现摄像头镜像翻转

    之前的项目需要镜像翻转摄像头视频,使用Aforge.Net来处理视频. 一开始考虑直接从Aforge.Net读取没一帧视频图片,然后复制给WPF的Image控件,这种方法基本很卡,所以放弃了. 考虑到 ...

  10. [转]delphi 删除动态数组的指定元素

    type TArr = array of TPoint; {把数组先定义成一个类型会方便许多, 这里仅用 TPoint 测试} {删除动态数组指定元素的过程: 参数 arr 是数组名, 参数 Inde ...