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. SpringMVC -- 梗概--源码--贰--静态资源的访问问题

    配置:<mvc:default-servlet-handler/> 1>静态资源:除了Servlet.Controller之外的资源,如:js,css,png,html等 2> ...

  2. Log4net用法(App.config配置)

    配置文件 <configSections> <section name="log4net" type="log4net.Config.Log4NetCo ...

  3. webstorm批量查找,批量替换快捷键

    ctrl+shift+f:批量查找,我的webstorm11不能用ctrl+shift+f进行批量查找了,不知道什么原因,自己又胡乱实验了一下, 发现ctrl+shift+g+f可以批量查找 ctrl ...

  4. 【NodeJS】http-server.cmd

    npm install http-server @echo off  start cmd /k "D:\Program Files\nodejs\node_global\http-serve ...

  5. 【Android】水平居中 垂直居中 中心居中

    android:layout_centerInParent 将该组件放置于水平方向中央及垂直中央的位置 android:layout_centerHorizontal 将该组件放置于水平方向中央的位置 ...

  6. WAF Bypass数据库特性(Mysql探索篇)

    0x01 背景 Mysql数据库特性探索,探索能够绕过WAF的数据库特性. 0x02 测试 常见有5个位置即:   SELECT * FROM admin WHERE username = 1[位置一 ...

  7. mssql注入指令

    and exists (select * from sysobjects) //判断是否是MSSQL and exists(select * from tableName) //判断某表是否存在..t ...

  8. Python 扩展知识

    Python 练习题 Python 编程习惯 Python 转义字符 Python 格式化输出 Python 列表表达式 Python 生成器表达式 Python 序列化 Python2 与 Pyth ...

  9. [Command] lrzsz - 文件传输工具包

    lrzsz 是一个支持 XMODEM.YMODEM.ZMODEM 文件传输协议的 Unix 程序包.它是 Omen Technologies 公司所有的 rzsz 程序包的公开发行增强版,遵守 GNU ...

  10. Elastic Search 5.4.3 java api 入门

    首先介绍一点,es的版本从之前的2.x跳跃到5.x,很多插件要保持一致,不然会产生很多版本不兼容的问题. 首先看一个demo先熟悉一下, 具体代码在git服务器上: https://github.co ...