poj3026(bfs+prim)最小生成树
Your task is to help the Borg (yes, really) by developing a program
which helps the Borg to estimate the minimal cost of scanning a maze for
the assimilation of aliens hiding in the maze, by moving in north,
west, east, and south steps. The tricky thing is that the beginning of
the search is conducted by a large group of over 100 individuals.
Whenever an alien is assimilated, or at the beginning of the search, the
group may split in two or more groups (but their consciousness is still
collective.). The cost of searching a maze is definied as the total
distance covered by all the groups involved in the search together. That
is, if the original group walks five steps, then splits into two groups
each walking three steps, the total distance is 11=5+3+3.
Input
the first line of input there is one integer, N <= 50, giving the
number of test cases in the input. Each test case starts with a line
containg two integers x, y such that 1 <= x,y <= 50. After this, y
lines follow, each which x characters. For each character, a space ``
'' stands for an open space, a hash mark ``#'' stands for an obstructing
wall, the capital letter ``A'' stand for an alien, and the capital
letter ``S'' stands for the start of the search. The perimeter of the
maze is always closed, i.e., there is no way to get out from the
coordinate of the ``S''. At most 100 aliens are present in the maze, and
everyone is reachable.
Output
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
Source
代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int map[300][300],dis[300],vis[300];
char str[300][300];
int point[300][300];
int tvis[300][300],tdis[300][300];
struct node{
int x,y;
};
int m,n,ans;
int tnext[4][2]={1,0,0,1,-1,0,0,-1};
void bfs(int tx,int ty){
queue<node>q;
node next,res;
memset(tvis,0,sizeof(tvis));
memset(tdis,0,sizeof(tdis));
tvis[tx][ty]=1;
res.x=tx;
res.y=ty;
q.push(res);
while(!q.empty()){
res=q.front();
q.pop();
if(point[res.x][res.y]){
map[point[tx][ty]][point[res.x][res.y]]=tdis[res.x][res.y];
}
int xx,yy;
for(int k=0;k<4;k++){
next.x=xx=res.x+tnext[k][0];
next.y=yy=res.y+tnext[k][1];
if(xx>=1&&xx<=m&&yy>=1&&yy<=n&&!tvis[xx][yy]&&str[xx][yy]!='#'){
tvis[xx][yy]=1;
tdis[xx][yy]=tdis[res.x][res.y]+1;
q.push(next);
}
}
}
}
int prim(int u){
int sum=0;
for(int i=1;i<=ans;i++){
dis[i]=map[u][i];
}
vis[u]=1;
for(int ti=2;ti<=ans;ti++){
int tmin=2000000000;
int k;
for(int i=1;i<=ans;i++){
if(dis[i]<tmin&&!vis[i]){
tmin=dis[i];
k=i;
}
}
sum+=tmin;
vis[k]=1;
for(int j=1;j<=ans;j++){
if(dis[j]>map[k][j]&&!vis[j])
dis[j]=map[k][j];
}
}
return sum;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(point,0,sizeof(point));
memset(map,0,sizeof(map));
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(str,0,sizeof(str));
scanf("%d%d",&n,&m);
gets(str[0]);
ans=0;
for(int i=1;i<=m;i++){
gets(str[i]+1);
for(int j=1;j<=n;j++){
if(str[i][j]=='S'||str[i][j]=='A')
point[i][j]=++ans;
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(point[i][j])
bfs(i,j);
}
}
printf("%d\n",prim(1));
}
return 0;
}
poj3026(bfs+prim)最小生成树的更多相关文章
- 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)
1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...
- Prim 最小生成树算法
Prim 算法是一种解决最小生成树问题(Minimum Spanning Tree)的算法.和 Kruskal 算法类似,Prim 算法的设计也是基于贪心算法(Greedy algorithm). P ...
- dijkstra(最短路)和Prim(最小生成树)下的堆优化
dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于 ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- poj3026(bfs+prim)
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- POJ3026(BFS + prim)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10554 Accepted: 3501 Descri ...
- POJ 3026(BFS+prim)
http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
随机推荐
- vue.esm.js:578 [Vue warn]: Missing required prop
问题: 解决: required: true,属性是,这个必须填写
- hdu_3123_GCC
The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Proj ...
- solr索引大小对比
原文本 Solr建立的索引 如果进行Mysql索引应该是1:3的比例
- Tools - GitBook
GitBook图书绑定自定义的域名 https://jingyan.baidu.com/article/335530daf86c3b19cb41c3f3.html
- android 自定义图片圆形进度条
感觉话一个圆形进度条挺简单的 ,但是却偏偏给了几张图片让你话,说实话我没接触过,感觉好难,还好百度有大把的资源,一番努力下终于画出来了. 代码如下. package com.etong.cpms.wi ...
- web前端总结面试问题(理论)
一个页面从输入url到页面显示加载完成,这个过程发生了什么? 1.浏览器根据请求的URL交给DNS域名解析,找到真实的IP,向服务器发起请求. 2.服务器交给后台处理完成后返回数据,浏览器接收文件(h ...
- jQuery(四)--HTTP请求
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 主流浏览器内核,以及CSS3前缀识别码
现在国内常见的浏览器有:IE.Firefox.QQ浏览器.Safari.Opera.Google Chrome.百度浏览器.搜狗浏览器.猎豹浏览器.360浏览器.UC浏览器.遨游浏览器.世界之窗浏览器 ...
- QQ群技术:0成本创建2000人QQ群技巧
群人数,直接关系群权重;于排名,意义非凡;此法靠谱,笔者亲测. 就说这张图,这类关键词,要是没2000人群,不管你多流弊,你是做不上去滴. 于QQ群霸屏,笔者有太多的笔墨,各种排名技巧,阿力推推早前明 ...
- dotnet core 数据库
dotnet core 数据库 程序开发过程中,需要使用数据对数据进行存储,分析等.通常而言都会使用ORM来实现关系数据库与实体对象的转化,过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持 ...