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)的更多相关文章

  1. J - Borg Maze - poj 3026(BFS+prim)

    在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...

  2. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  3. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  4. poj3026(bfs+prim)

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

  5. poj3026(bfs+prim)最小生成树

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

  6. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  7. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  8. POJ 3669 Meteor Shower (BFS+预处理)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  9. Full Tank? POJ - 3635 (bfs | 最短路)

    After going through the receipts from your car trip through Europe this summer, you realised that th ...

随机推荐

  1. 好看的CSS按钮

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. 在CentOS上安装ZooKeeper集群

    一共准备3个CentOS虚拟机 172.16.9.194 172.16.9.195 172.16.9.196 上传zookeeper-3.3.6.tar.gz到服务器并解压,3台服务器的目录结构如下 ...

  3. servlet request getHeader(“x-forwarded-for”) 获取真实IP

    request方法客户端IP: request.getRemoteAddr() 输出:192.168.0.106 客户端主机名:request.getRemoteHost()输出:abc reques ...

  4. SQL Server2008 MERGE指令用法

    参考资料: 百度百科-MERGE

  5. [慢查优化]联表查询注意谁是驱动表 & 你搞不清楚谁join谁更好时请放手让mysql自行判定

    写在前面的话: 不要求每个人一定理解 联表查询(join/left join/inner join等)时的mysql运算过程: 不要求每个人一定知道线上(现在或未来)哪张表数据量大,哪张表数据量小: ...

  6. django学习<一>:安装

    这两天打算摸索下和python相关的东西,然后正好小伙伴有个关于网站的任务,就怀着好奇的心态了解了下,然后就很自然地开始涉及django的问题. 首先就是django安装的问题,想不到第一步就出问题了 ...

  7. VTK初学一,c_Line_CellArray线段的CellArray绘制

    VTK窗口默认坐标方向: #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE ...

  8. spark

    http://www.cnblogs.com/shishanyuan/p/4723604.html?utm_source=tuicool spark presto2.0计算引擎 http://blog ...

  9. 2013区域赛长沙赛区现场赛 K - Pocket Cube

    K - Pocket Cube Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  10. Spring配置bean文件的底层实现方式

    首先 bean文件如下: <beans> <bean id="date" class="java.util.Date"></bea ...