题目链接

题目大意:

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

分析:

第一感觉使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. UVA 11212 IDA*

    移动一块连续的区间使得数列递增.问最少次数. 直接IDA*暴搜,只是我没有想到A*函数,所以就随手写了个连续递增块数作为估价函数,WA了,然后除以2,还是WA,除以3,WA,除以4...过了= = # ...

  2. eclipse设置系统字体

    1. 打开eclipse-->Window-->Preferences-->General-->appearance-->Colors and Fonts, 点开后选择B ...

  3. troubleshooting tools in JDK 7--转载

    This chapter describes in detail the troubleshooting tools that are available in JDK 7. In addition, ...

  4. mac 查找当前目录下所有同一类型文件,并执行命令行

    以TexturePacker举例 MAC下用TexturePacker命令行打包当前目录下所有的 *.tps文件 1.配置好tps文件需要配置好路径.参数等.(也可不配置,用命令行实现.具体参考:ht ...

  5. Xml解析之——Java/Android/Python

    Xml解析之——Java/Android/Python 一.Xml文件 test.xml <note> <to>George</to> <from>Jo ...

  6. Sqlite 错误码

    #define SQLITE_OK 0 /* 成功 | Successful result */ /* 错误码开始 */ #define SQLITE_ERROR 1 /* SQL错误 或 丢失数据库 ...

  7. ajax页面数据的传递

    在上一篇文章中,简单提到了ajax的工作流程,那么在这里我们就得实战一回了,真正将ajax的用途展现出来,这一整套流程就是在页面上触发一个ajax事件,然后发送请求,紧接着到数据库读取数据,返回值,然 ...

  8. cas sso单点登录系列3_cas-server端配置认证方式实践(数据源+自定义java类认证)

    转:http://blog.csdn.net/ae6623/article/details/8851801 本篇将讲解cas-server端的认证方式 1.最简单的认证,用户名和密码一致就登录成功 2 ...

  9. Cloudera Manager(CentOS)安装介绍

    相信通过这篇文章大家都对Cloudera Manager及CDH安装有一个整体的认识 目 录 1           准备工 作.................................... ...

  10. 下拉菜单选择(jQuery实现)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...