POJ 3026(BFS+prim)
http://poj.org/problem?id=3026
题意:任意两个字母可以连线,求把所有字母串联起来和最小。
很明显这就是一个最小生成树,不过这个题有毒。他的输入有问题。在输入m和N后面,可能有一大串的空格。就因为这个,我RE都有点懵了,要不是discuss里面有人说输入有问题,我都没注意到这个,原本只用了一个getchar吃掉最后的换行符。没想到之后还有空格。有点小坑。
思路:这个题目如果他给你一个图,那就是最裸的prim了。不过这个题的难点也就是在他给的图你不能用Prim,你只能通过bfs去建立一个你可以新图,让这个新图去使用最小生成树。
#include <string.h>
#include <stdio.h>
#include <queue>
#define inf 0x3f3f3f
#define maxn 300 using namespace std; char mgraph[ maxn ][ maxn ];
int bfsgraph[ maxn ][ maxn ],dist[ maxn ][ maxn ],num[ maxn ][ maxn ],ans,dis[ maxn ];
bool mark[ maxn ][ maxn ],vis[ maxn ]; struct note{
int x,y;
}; void bfs(int x,int y) //我是把图建在bfsgraph里面的。
{
memset( dist , ,sizeof( dist ) );
memset( mark , true , sizeof( mark ) );
bfsgraph[ num[ x ][ y ] ][ num[ x ][ y ] ] = ;
queue<note >s;
note p,q;
p.x = x;
p.y = y;
dist[ x ][ y ] = ;
mark[ x ][ y ] = false;
s.push(p);
while(!s.empty())
{
p = s.front();
s.pop();
if(mark[ p.x + ][ p.y ] && mgraph[ p.x + ][ p.y ] != '#')
{
if( num[ p.x + ][ p.y ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x+ ][ p.y ]] = dist[ p.x ][ p.y ] + ; //如果当前点为字母的话,那么把这个点与起始点的距离给记录。
dist[ p.x + ][ p.y ] = dist[ p.x ][ p.y ] + ;
mark[ p.x + ][ p.y ] = false;
q.x = p.x + ;
q.y = p.y;
s.push(q);
}
if(mark[ p.x ][ p.y - ] && mgraph[ p.x ][ p.y - ] != '#')
{
if( num[ p.x ][ p.y - ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x ][ p.y - ]] = dist[ p.x ][ p.y ] + ;
dist[ p.x ][ p.y - ] = dist[ p.x ][ p.y ] + ;
mark[ p.x ][ p.y - ] = false;
q.x = p.x;
q.y = p.y - ;
s.push(q);
}
if(mark[ p.x ][ p.y + ] && mgraph[ p.x ][ p.y + ] != '#')
{
if( num[ p.x ][ p.y + ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x ][ p.y + ]] = dist[ p.x ][ p.y ] + ; dist[ p.x ][ p.y + ] = dist[ p.x ][ p.y ] + ;
mark[ p.x ][ p.y + ] = false;
q.x = p.x;
q.y = p.y + ;
s.push(q);
}
if(mark[ p.x - ][ p.y ] && mgraph[ p.x - ][ p.y ] != '#')
{
if( num[ p.x- ][ p.y ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x - ][ p.y ]] = dist[ p.x ][ p.y ] + ;
dist[ p.x - ][ p.y ] = dist[ p.x ][ p.y ] + ;
mark[ p.x - ][ p.y ] = false;
q.x = p.x - ;
q.y = p.y;
s.push(q);
}
}
}
int prim(int pos)
{
for(int i = ; i < pos ; i++)
dis[ i ] = inf ; dis[ ] = ;
for(int i = ;i < pos ; i++){
int tep = inf;int k = ;
for(int j = ; j < pos ; j++){
if(vis[ j ]&&dis[ j ]<tep)
{
tep = dis[ j ];
k = j;
}
}
if(tep == inf) return ;
ans += tep;
vis[ k ]=false;
for(int j = ;j < pos ; j++)
if(vis[ j ]&&dis[ j ] > bfsgraph[ k ][ j ])
dis[ j ] = bfsgraph[ k ][ j ];
}
return ;
} int main()
{
// freopen("in.txt","r",stdin);
int t,m,n;
char s[];
scanf("%d",&t);
while( t-- )
{
memset( mgraph , , sizeof( mgraph ) );
memset( bfsgraph , , sizeof( bfsgraph ) );
memset( num , , sizeof( num ) );
scanf("%d%d",&m,&n);
gets(s); //这里要注意,要吃掉m,n后面的空格,不然就等着RE吧。
ans = ;
int pos = ;
note point[ ];
for( int i = ; i < n ; i++ )
gets(mgraph[i]);
for( int i = ; i < n ; i++ )
for( int j = ; j < m ; j++ )
if( mgraph [ i ][ j ] == 'A' || mgraph[ i ][ j ] == 'S')
{
point[ pos ].x = i;
point[ pos ].y = j;
num[ i ][ j ] = pos; //我用num来编号,相当于一个映射,一个点集与一个数集之间的映射,因为要建图。
pos ++;
}
for( int i = ; i < pos ; i++ ) //对每一个点进行bfs。
bfs( point[ i ].x , point[ i ].y );
memset( vis , true , sizeof( vis ) );
prim(pos);
printf("%d\n",ans);
}
return ;
}
POJ 3026(BFS+prim)的更多相关文章
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 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. ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- Full Tank? POJ - 3635 (bfs | 最短路)
After going through the receipts from your car trip through Europe this summer, you realised that th ...
随机推荐
- (HBase) Error: JAVA_HOME is not set and Java could not be found
请查看Linux系统环境变量 JAVA_HOME 是否设置.. 两种方法 env //查看全部系统变量 或者 echo $JAVA_HOME //查看JAVA_HOME变量值 如果没有设置则输出空 没 ...
- Python开发【第十七篇】:MySQL(一)
一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...
- Druid使用起步—在javaWeb项目中配置监控 连接池
当我们在javaWEB项目中使用到druid来作为我们的连接池的时候,一定不会忘了添加监控功能.下面我们就来看一下,在一个简单的web项目中(尚未使用任何框架)我们是如果来配置我们的web.xml来完 ...
- intelligencia.urlrewriter使用
见github: https://github.com/sethyates/urlrewriter
- [工具]Swagger-api接口文档描述
摘要 工作中经常的用到webapi,之前都是提供的使用postman模拟请求的截图,非常的不方便,如果能在项目中集成一个在线查看接口说明的地方,肯定更方便更直观.在网上看到swagger这个组件,界面 ...
- 性能:15个JavaScript本地存储技术的函数库和工具
当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...
- jquery eval解析JSON中的注意点介绍
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式:使用eval()函数.使用Function对象来进行返回解析,下面有个示例,感兴趣的朋友可以参考下 在JS中将JSON的字符串解析 ...
- ExtJS + fileuploadfield上传文件代码
后台服务端接收文件的代码: /** * 后台上传文件处理Action */ @RequestMapping(value = "/uploadFile", method=Reques ...
- js DOM Element属性和方法整理
节点操作,属性 1. childNodes.children 这两个属性获取到的子节点会根据浏览器的不同而不同的,所以一定要判断下nodeType是否为1. childNodes获取到的是NodeLi ...
- POJ 2054 Color a Tree
贪心.... Color a Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: ...