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 ...
随机推荐
- c# 分析SQL语句中的表操作
最近写了很多方向的总结和demo.基本包含了工作中的很多方面,毕竟c#已经高度封装并且提供了很多类库.前面已经总结了博文.最近2天突然感觉前面的SQL分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...
- JavaScript 基础(三) 对象 条件判断
JavaScript的对象是一种无序的集合数据类型,它是由若干键对组成. var guagua = { name:'瓜瓜', birth:1988, school:'No.1 Middle Schoo ...
- POJ 1180 Batch Scheduling (dp,双端队列)
#include <iostream> using namespace std; + ; int S, N; int T[MAX_N], F[MAX_N]; int sum_F[MAX_N ...
- ABAP术语-Business Object Builder
Business Object Builder 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/09/1031357.html Tool fo ...
- JDBC配置文件db.properties(Mysql) 及dbutils的编写
#数据库驱动driver=com.mysql.jdbc.Driver#数据库连接url=jdb:mysql://localhost:3306/newdb3?useUnicode=true&ch ...
- Java写Excel(不生成实体文件,写为流的形式)
java 写 Excel(不生成实体文件,写为流的形式) public String exportReportExcel(String mediaCode, List<SimpleMediaRe ...
- tomcat+nginx+keepalived的配置
tomcat+nginx+keepalived的配置 1.在官网上下载Tomcat 2.将压缩包解压,并且移动到/opt/data/的目录下. .tar.gz /opt/data/ 3.进入到Tomc ...
- Python学习:1.快速搭建python环境
一.安装python 现在python有两个比较大的版本一个是python3.x一个是python2.x,python3.x相当于与python2.x是一个比较大的升级,但是python3.x没有向下 ...
- C errno是否是线程安全的
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/138 在使用多线程时,遇到了一个问题:线程例程中如果需要使用er ...
- shell重温---基础篇(shell数组&数组操作)
上篇博客已经分析重温了shell的运行方式以及其中的变量还有字符串,之后按照套路就是数组方面了,废话不多说,直接进入正题哈.(小白笔记,各位看官勿喷...) bash shell呢,支持一位数 ...