http://poj.org/problem?id=3026

题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地。求从S出发,经过所有A所需要的最短路。你有一个特殊能力,当走到S或A时可以分身出任意多个人一起走。计算路程时就是所有人的总路程之和。

题解:想一下,是裸的最短路套上bfs。

  先暴力bfs出各个点之间的距离,存边

  然后kruskal

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string.h>
#include<set>
#include<string>
using namespace std;
const int maxn = 3e4;;
int len1, len2, p[maxn], ans;
set<int> s;
struct edge {
int to, from, w;
edge(int to=, int from=, int w=) :to(to), from(from), w(w) {}
};
vector<edge> e;
bool cmp(edge a, edge b) {
return a.w < b.w;
}
int f[maxn];
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
void un(int x, int y) {
int u = find(x), v= find(y);
f[u] = v;
}
bool same(int x, int y) {
return find(x) == find(y);
}
string map[];
int id[][];
int vis[][];
int dir[][] = { , ,,, ,-, -, };
struct node {
int x, y, t;
node(int x=, int y=, int t = ) :x(x), y(y), t(t) {}
};
void bfs(int x, int y) {
memset(vis, , sizeof(vis));
queue<node> Q;
Q.push(node(x, y, )); vis[x][y] = ;
while (!Q.empty()) {
node now = Q.front();
Q.pop();
for (int i = ; i < ; i++) {
int dx = now.x + dir[i][];
int dy = now.y + dir[i][];
if (map[dx][dy] == '#'||vis[dx][dy]) continue;
if (map[dx][dy] == 'A')e.push_back(edge(id[x][y], id[dx][dy], now.t+));
Q.push(node(dx, dy, now.t + ));
vis[dx][dy] = ; }
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
scanf("%d %d\n", &n,&m);
for (int i = ; i < m; i++) {
getline(cin, map[i]);
}
memset(id, , sizeof(id));
e.clear();
int idx = ;
for(int i=;i<m;i++)
for (int j = ; j < n; j++) {
if (map[i][j] == 'S')map[i][j] = 'A';
if (map[i][j] == 'A' ) {
id[i][j] = idx++;
}
}
for (int i = ; i<m; i++)
for (int j = ; j < n; j++) {
if(id[i][j])bfs(i, j);
}
for (int i = ; i < idx; i++) { f[i] = i; }
sort(e.begin(), e.end(),cmp);
int res = ;
for (int i = ; i < e.size(); i++) {
if (same(e[i].to, e[i].from)) continue;
un(e[i].to, e[i].from);
res += e[i].w;
}
cout << res << endl; }
system("pause");
}

POJ - 3026 Borg Maze bfs+最小生成树。的更多相关文章

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

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

  2. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  3. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  4. POJ 3026 Borg Maze (最小生成树)

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

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

    题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...

  6. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  7. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

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

    https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...

  9. POJ 3026 Borg Maze【BFS+最小生成树】

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

随机推荐

  1. http 返回码 405 解决方案之一

    今天做网络请求数据的时候遇到返回码405,当时就傻了~~ 故事是这样的-- 我用post请求访问一个url,服务端数据是一个json的txt文件,理论上直接访问,返回json,然后解析就没事了,可是今 ...

  2. Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...

  3. Windows Server 2008 R2 WSUS服务器的详细配置和部署

    WSUS客户端配置 我们要让客户端计算机能够通过WSUS服务器下载更新程序,而这个设置在域环境和单台PC是的方法不同,这里介绍一下本地计算机如何进行设置. 1.开始--运行--输入gpedit.msc ...

  4. nessus 激活码

    nessus激活码的申请 nessus屏蔽了中国的激活码申请,中国IP申请的时候会直接跳转到购买商业版的页面. 解决方法: 使用IE代理或者VPN,用美国的IP最好,然后访问网址: http://ww ...

  5. PPT高手必须树立的十个理念

    08 2014年08月 [263职场技巧]PPT高手必须树立的十个理念 理念一:文字是用来瞟的,不是读的 我们时不时听到这样的言论:“PPT很简单,就是把Word里的文字复制.粘贴呗.”这其实是对PP ...

  6. hadoop应用开发技术详解

    <大 数据技术丛书:Hadoop应用开发技术详解>共12章.第1-2章详细地介绍了Hadoop的生态系统.关键技术以及安装和配置:第3章是 MapReduce的使用入门,让读者了解整个开发 ...

  7. Linux记录用户shell命令

    在/etc/profile中添加下面内容: export LC_ALL=C TMOUT=3600 HISTFILESIZE=2000 HISTSIZE=2000 HISTTIMEFORMAT=&quo ...

  8. ipcs命令以及oracle内存段

    今天是2014-01-06,在没过春节之前重新复习一下2013年学习的内容,关于oracle内存段在我之前写的blog中有详细操作.在此记录一下ipcs命令的用法. http://blog.csdn. ...

  9. Android Studio 删除 Module

    1.选中Module右击,选择 Open Module Settings,打开Project Structure 窗空.(或者选中Module,按F4打开Project Structure窗口) 2. ...

  10. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...