POJ3026 最小生成树
问题: POJ3026
分析:
采用BFS算出两两之间的距离,再用PRIM算法计算最小生成树。
AC代码:
//Memory: 220K Time: 32MS
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
;
;
char maze[maxn][maxn];
int m[maxn][maxn];
int g[max_alien][max_alien];
int vis[maxn][maxn];
];
];
int alien;
int test, x, y;
], sj[max_alien + ];
][] = {{, }, {, -}, {, }, {-, }};
struct node
{
int i;
int j;
int d;
void set(int ii, int jj)
{
i = ii;
j = jj;
d = ;
}
}n1, n2;
queue<struct node> q;
void bfs(int num, int i, int j)
{
memset(vis, , sizeof(vis));
int t = alien - num;
while ( !q.empty() ) q.pop();
n1.set(i, j);
q.push(n1);
){
n1 = q.front();
q.pop();
; i < ; i++){
n2.], n1.j + step[i][]);
/*if (n2.i < 0 || n2.j < 0 ||)*/
if (maze[n2.i][n2.j] == '#' || vis[n2.i][n2.j]) continue;
vis[n2.i][n2.j] = ;
n2.d = n1.d + ;
if (m[n2.i][n2.j] > num){
t--;
g[num][ m[n2.i][n2.j] ] = g[ m[n2.i][n2.j] ][num] = n2.d;
}
q.push(n2);
}
}
}
int prim()
{
memset(v, , sizeof(v));
v[] = ;
;
; i <= alien; i++)
d[i] = g[][i];
; i < alien; i++) {
, ix;
; j <= alien; j++) {
if ( !v[j] && d[j] < _min){
_min = d[j];
ix = j;
}
}
v[ix] = ;
ret += d[ix];
; j <= alien; j++){
if (!v[j] && d[j] > g[ix][j])
d[j] = g[ix][j];
}
}
return ret;
}
int main()
{
scanf("%d", &test);
while (test--){
memset(m, , sizeof(m));
scanf("%d%d", &x, &y);
gets(maze[]);
alien = ;
; i < y; i++)
gets(maze[i]);
; i < y; i++){
; j < x; j++){
if (maze[i][j] == 'S'){
si[] = i;
sj[] = j;
}
else if (maze[i][j] == 'A') {
m[i][j] = ++alien;
si[alien] = i;
sj[alien] = j;
}
}
}
memset(g, , sizeof(g));
; i <= alien; i++){
bfs(i, si[i], sj[i]);
}
int ans = prim();
printf("%d\n", ans);
}
;
}
POJ3026 最小生成树的更多相关文章
- POJ-3026 Borg Maze---BFS预处理+最小生成树
题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S ...
- POJ-3026(图上的最小生成树+prim算法+gets函数使用)
Borg Maze POJ-3026 一开始看到这题是没有思路的,看了题解才知道和最小生成树有关系. 题目的意思是每次走到一个A或者S就可以分为多个部分继续进行搜索.这里就可以看出是从该点分出去的不同 ...
- POJ3026 Borg Maze(最小生成树)
题目链接. 题目大意: 任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小. 分析: 第一感觉使3维的BFS.但写着写着,发现不对. 应当用最小生成树解法.把每个字母(即A,或S)看成一个结 ...
- POJ3026:Borg Maze (最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18644 Accepted: 5990 题目链接:h ...
- POJ3026 Borg Maze(bfs求边+最小生成树)
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- poj3026(bfs+prim)最小生成树
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- 最小生成树练习3(普里姆算法Prim)
风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...
- POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...
随机推荐
- Javascript兼容收集
1.IE6背景缓存 try{ document.execCommand("BackgroundImageCache", false, true); }catch(e){} 2. e ...
- python学习第二天 --变量及其字符串
python变量: 在计算机程序中,变量不仅可以是数字,还可以是任意数据类型. 在Python程序中,变量是用一个变量名表示,变量名必须是大小写英文.数字和下划线(_)的组合,且不能用数字开头. 在P ...
- 【转】ios开发之生成所缩略图方式
亲测:两种方式都有效 第一种方式:缩略成固定的尺寸大小 - (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSiz ...
- (摘) MDI登陆问题
MDI编程中需要验证用户身份,那么登陆窗口就需要在验证密码后进行相关的隐藏处理. (1)隐藏登陆窗口(登陆窗体作为启动) 登陆按钮事件:this.Hide();//隐藏登陆窗口MDI_Name M = ...
- hdu 3191 How Many Paths Are There
http://acm.hdu.edu.cn/showproblem.php?pid=3191 这道题求次短路经和路径数 #include <cstdio> #include <cst ...
- 《数据通信与网络》笔记--SSL/TLS
上次简单地介绍了IP层的安全,今天来介绍下在传输层提供安全性最主要的2个协议:安全套接字层(SSL)协议和传输 层安全(TLS)协议.TLS实际上就是SSL的IETF版本. 1.SSL服务 设计安全套 ...
- logstash date插件介绍
时间处理(Date) 之前章节已经提过, filters/date 插件可以用来转换你的日志记录中的时间字符串,变成 LogStash::Timestamp 对象,然后转存到 @timestamp 字 ...
- Apache本地服务无法自启动问题解决
问题描述: Windows不能在本地计算机启动Apache2.有关更多信息,查阅系统事件日志.如果这是非Microsoft服务,请与服务厂商联系,并参考特定服务错误代码1. 解决方案: 1.检查Apa ...
- UVa12096.The SetStack Computer
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- <转载>僵尸进程
转载http://www.cnblogs.com/scrat/archive/2012/06/25/2560904.html 什么是僵尸进程 僵尸进程是指它的父进程已经退出(父进程没有等待(调用wai ...