题意:

求把S和所有的A连贯起来所用的线的最短长度。。。

这道题。。不看discuss我能wa一辈子。。。

输入有坑。。。

然后,,,也没什么了。。。还有注意 一次bfs是可以求当前点到所有点最短距离的。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int f[maxn], d[][];
int cnt, n, m;
int dis[][] = {{,},{-,},{,},{,-}};
char str[][], temp[maxn];
struct node
{
int u, v, w;
}Node[maxn]; struct edge
{
int x, y;
}Edge[maxn]; void add(int u, int v, int w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt++].w = w;
} void init()
{
for(int i=; i<maxn; i++) f[i] = i;
cnt = ;
} int cmp(node a, node b)
{
return a.w < b.w;
} int find(int x)
{
// return f[x]==x?x:(f[x] = find(f[x]));
if(f[x] == x) return x;
int r = f[x];
while(r != f[r]) r = f[r];
int i = x, j;
while(i!=r)
{
j = f[i];
f[i] = r;
i = j;
}
return r;
} void bfs(edge s)
{
queue<edge> Q;
mem(d, );
Q.push(s);
d[s.x][s.y] = ;
while(!Q.empty())
{
s = Q.front(); Q.pop();
for(int i=; i<; i++)
{
edge t;
t.x = s.x + dis[i][];
t.y = s.y + dis[i][];
if(t.x < || t.x >= n || t.y < || t.y >= m || d[t.x][t.y] || str[t.x][t.y] == '#') continue;
d[t.x][t.y] = d[s.x][s.y] + ;
Q.push(t);
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int ans = ;
scanf("%d%d",&m,&n);
gets(temp);
// getchar();
for(int i=; i<n; i++)
{
gets(str[i]);
for(int j=; j<m; j++)
{
if(str[i][j] == 'A' || str[i][j] == 'S')
Edge[++ans].x = i, Edge[ans].y = j;
}
}
for(int i=; i<=ans; i++)
{
bfs(Edge[i]);
for(int j=i+; j<=ans; j++)
add(i, j, d[Edge[j].x][Edge[j].y] - ); }
sort(Node, Node+cnt, cmp);
int sum = ;
for(int i=; i<cnt; i++)
{
int r = find(Node[i].u);
int l = find(Node[i].v);
if(r == l) continue;
f[r] = l;
sum += Node[i].w;
} printf("%d\n",sum); } return ;
}

Borg Maze POJ - 3026 (BFS + 最小生成树)的更多相关文章

  1. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  2. poj 3026(BFS+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 Descri ...

  3. POJ 3026 Borg Maze 广搜(BFS)+最小生成树

    题意:从S出发,去抓每一个A,求总路径最短长度.在S点和A点人可以分身成2人,不过一次只能让一个人走. 思路是先利用BFS求出各点之间的距离,建成图,再套用最小生成树模板. 一次性A了.不过觉得在判断 ...

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

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

  5. J - Borg Maze - poj 3026(BFS+prim)

    在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...

  6. Borg Maze poj 3026

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  7. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  8. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  9. POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

随机推荐

  1. maven 插

    一.maven插件元素 <?xml version="1.0" encoding="utf-8"?> <plugin> <!--插 ...

  2. MFC入门(三)-- MFC图片/文字控件(循环显示文字和图片的小程序)

    惯例附上前几个博客的链接: MFC入门(一)简单配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入门(二)读取输入字符:http ...

  3. Perhaps you are running on a JRE rather than a JDK

    在Eclipse中跑maven项目时,出现上面的问题: 1.有可能你的环境变量配置是在jre上面的,所以你要检查一下你配置文件,PATH和CLASSPATH都要检查 2.eclipse默认是跑在jre ...

  4. HNOI2019 爆零记

    HNOI2019爆零记 day \(-inf\) ~ day \(0\) 开学一周之后才停的课,停课之后就开始每天被包菜.我三月份几乎没有更博,就是因为每天都被虐的自闭了. day \(0\) 本来是 ...

  5. RuntimeError: Python is not installed as a framework 错误解决方案

    在virtualenv环境下使用matplotlib绘图时遇到了这样的问题: >>> import matplotlib.pyplot as pltTraceback (most r ...

  6. SpringBoot笔记

    官网: http://springboot.fun/ 收集到一个比较全的: https://blog.csdn.net/xiaoyu411502/article/details/52474037 Id ...

  7. 前端安全之XSS

    XSS定义 XSS, 即为(Cross Site Scripting), 中文名为跨站脚本, 是发生在目标用户的浏览器层面上的,当渲染DOM树的过程成发生了不在预期内执行的JS代码时,就发生了XSS攻 ...

  8. PairWork-电梯调度程序结对编程【附加题】

    1 接口改进 1) 之前判断电梯是否闲置的函数不太好理解,重新修改了,如下所示: //是否停顿状态(停止的以及开门间隔>=0) public bool IsIdle { get { return ...

  9. M1/M2 总结

    时光是一列不会回头的列车. 这一学期这么快就过去了,当时刚开始软件工程的那些日子还历历在目.不知道那些如风般过去的日子带给我了什么.然而我又清楚地认识到自己已经改变了. 刚开始软件工程的时候,我对团队 ...

  10. 第七周 linux如何装载和启动一个可执行文件

    潘恒 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验内容 1.预处理. ...