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 ...
随机推荐
- QT入门
QT += core gui widgets //引入需要用到的库 qDebug()<<"t="<<t<<QTime::currentTime( ...
- OS命令注入
SSL http://kb.cnblogs.com/page/162080/ http://baike.baidu.com/link?url=jPitKuDw_ncDlMbOc1SkWzM9TuKX9 ...
- centos 命令大全
文件操作: ls ####查看目录中的文件#### ls -F ####查看目录中的文件#### ls -l ####显示文件和目录的详细资料#### ls -a ...
- java编程中Properties类的具体作用和使用
如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的.(如清单 1 所示).最近更新的java.util.Properti ...
- JaxWsProxyFactoryBean 与 JaxWsDynamicClientFactory
1. JaxWsProxyFactoryBean 简介:调用方式采用了和RMI类似的机制,即客户端直接调用服务器端提供的服务接口(interface),CXF通过运行时代理生成远程服务的代理对象 ...
- Kafka入门经典教程
本帖最后由 desehawk 于 2015-5-3 00:45 编辑问题导读 1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic.发送消息.消费消息?3.如何书写Kafka程 ...
- json的中括号和大括号的使用?
参考这篇文章: http://www.cnblogs.com/sgdkg/archive/2012/12/03/2799723.html json 变量有两种可能, 可能是一个对象, (类似 类的实例 ...
- linux网络:常用命令(一)
1.ifconfig 可以查看Linux的网卡情况 ifconfig eth0 查看 eth0的信息 给一块网卡设置多个ip地址: ifconfig eth0:0 192.168.1.12 255. ...
- CF461B Appleman and Tree (树DP)
CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time l ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...