J - Borg Maze - poj 3026(BFS+prim)
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; int dir[][] = { {,},{,},{-,},{,-} };
char G[maxn][maxn]; //保存地图
int D[maxn][maxn]; //记录两点间的距离
int use[maxn][maxn]; //标记地图
int Index[maxn][maxn]; //记录‘A’或者‘B’的编号
struct node{int x, y, step;}; void BFS(int k, int M,int N, int x, int y)
{
queue<node> Q;
node s;
s.x = x, s.y = y, s.step = ;
use[s.x][s.y] = k; Q.push(s); while(Q.size())
{
s = Q.front();Q.pop();
if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z')
D[k][ Index[s.x][s.y] ] = s.step; for(int i=; i<; i++)
{
node q = s;
q.x += dir[i][], q.y += dir[i][]; if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && use[q.x][q.y]!=k)
{
use[q.x][q.y] = k;
q.step += ;
Q.push(q);
}
}
}
}
int Prim(int N) //这里面的N代表编号最多到N
{
int i, dist[maxn], vis[maxn]={, };
int ans = , T=N-; for(i=; i<=N; i++)
dist[i] = D[][i]; while(T--)
{
int k=, mini = oo; for(i=; i<=N; i++)
{
if(!vis[i] && mini > dist[i])
mini = dist[i], k=i;
}
ans += mini;
vis[k] = true; for(i=; i<=N; i++)
if(!vis[i])dist[i] = min(dist[i], D[k][i]);
} return ans;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, j, M, N, t=; scanf("%d%d ", &N, &M); for(i=; i<M; i++)
{
gets(G[i]);
for(j=; j<N; j++)
{
if(G[i][j]>='A' && G[i][j]<='Z')
Index[i][j] = t++;
use[i][j] = ;
}
} for(i=; i<M; i++)
for(j=; j<=N; j++)
{
if(G[i][j]>='A' && G[i][j]<='Z')
BFS(Index[i][j], M, N, i, j);
} int ans = Prim(t-); printf("%d\n", ans);
} return ;
}
J - Borg Maze - poj 3026(BFS+prim)的更多相关文章
- POJ 3026(BFS+prim)
http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
- (最小生成树) Borg Maze -- POJ -- 3026
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- Borg Maze - poj 3026(BFS + Kruskal 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9821 Accepted: 3283 Description The B ...
- Borg Maze POJ - 3026 (BFS + 最小生成树)
题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...
- Borg Maze poj 3026
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- poj3026(bfs+prim)
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- poj3026(bfs+prim)最小生成树
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
随机推荐
- Linux shell入门基础(八)
八.shell脚本sed&awk 01.sed的使用 流编辑器-Steam Editor #ed /etc/passwd 1,10p …… 1s/root/byf/p(替换root为byf) ...
- android应用的不同版本间兼容性处理
在Android系统中向下兼容性比较差,但是一个应用APP经过处理还是可以在各个版本间运行的.向下兼容性不好,不同版本的系统其API版本也不同,自然有些接口也不同,新的平台不能使用旧的API,旧的平台 ...
- IIS相关问题
问题:使用vs开发项目完成后,发布在本地IIS上,访问链接出现如下情况: 解决方案:打开IIS--->>
- Android permission访问权限大全
1.android.permission.WRITE_USER_DICTIONARY 允许应用程序向用户词典中写入新词 2.android.permission.WRITE_SYNC_SETTINGS ...
- 该项目中不存在目标 precomputecompiletypescript The target "PreComputeCompileTypeScript" does not exist in the project
Open Microsoft.TypeScript.targets file located under C:\Program Files (x86)\MSBuild\Microsoft\Visual ...
- [LeetCode OJ] Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【USACO 2.4.5】分数化小数
[描述] 写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中. 例如, 1/3 =0.33333333 写成0.(3), 4 ...
- Build Android-x86 ICS 4 Virtualbox from Google Virtualbox Target and Intel Kernel 编译体验
最近一直在研究android源码的编译,应该说研究的很辛苦,最难的是下源码,总是不停的断掉,最后感谢公司的高网速,找到方法后12G的源码只花了1个小时就下完了. 参考以下网址:http://softw ...
- Linux系统中,main函数的执行过程
http://blog.csdn.net/rrerre/article/details/6728431
- java面试题及答案(基础题122道,代码题19道)
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...