POJ3026 Borg Maze(最小生成树)
题目链接。
题目大意:
任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小。
分析:
第一感觉使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(最小生成树)的更多相关文章
- POJ3026:Borg Maze (最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18644 Accepted: 5990 题目链接:h ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14165 Accepted: 4619 Descri ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ3026 Borg Maze(Prim)(BFS)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12729 Accepted: 4153 Descri ...
- POJ3026 Borg Maze(bfs求边+最小生成树)
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- (POJ 3026) Borg Maze 最小生成树+bfs
题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...
- poj 3026 Borg Maze (最小生成树+bfs)
有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...
随机推荐
- hdu 3642 Get The Treasury(扫描线)
pid=3642" style="">题目链接:hdu 3642 Get The Treasury 题目大意:三维坐标系,给定若干的长方体,问说有多少位置被覆盖3次 ...
- intent和intentfilter
intent 和intent Filters startActivity()的机制 用到了IBinder ipc 用到了进程间通讯机制 activity有四种LaunchMode 当startActi ...
- PHP CodeBase: 判断用户是否手机访问(转)
随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面.这里就介绍 ...
- eclipse不能识别虚拟机的问题
1.输入cmd进入dos界面,进入android-sdk-windows\platform-tools目录,执行下面命令启动adb start-server出现下面错误* daemon not run ...
- sql查阅每一月的数据
因为项目中需要做数据报表的功能,需要统计每个月的销售额.我找到下面的sql语句.后来经过自己的测试,发现第二句才是可以用的, //String sql="SELECT year(buydat ...
- .NET获取机器信息
/// <summary> /// using System.Web; /// using System.Management; /// </summary> public s ...
- ICOMOON!强悍的WEB字体图标制造器/Web字体使用实例
IcoMoon!一个可以通过个性化设置来创建自定义图标(字体)的生成器!也可以将SVG图片,转换成web字体 IcoMoon是一项免费的服务,通过使用不同设置使我们能够创建自定义的ICON图或ICON ...
- 【转】自动布局之autoresizingMask使用详解(Storyboard&Code)
原文:http://www.cocoachina.com/ios/20141216/10652.html 自动布局Autolayoutstoryboard 前言:现在已经不像以前那样只有一个尺寸,现在 ...
- CSS样式的优势
为什么使用css样式来设置网页的外观样式呢?下面是一段文字,我们想把“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短语的文本颜色设置为红色,这时就 可以通过设置样式来设置,而且只需要编写一条c ...
- 六、C# 派生
派生 对一个现有的类型进行扩展,以便添加更多的功能,或者对现有的类型的操作进行重写. 比如可以将两个类都适用的方法和属性,用一个新的类进行重构,两个类再分别继承这个类. 定义一个派生类时,要在 ...