第四届 山东省ACM B Thrall’s Dream(连通图 OR BFS)

2023-10-30 23:20

本文主要是介绍第四届 山东省ACM B Thrall’s Dream(连通图 OR BFS),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Thrall’s Dream

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

We never paid any heed to the ancient prophecies, like fools we clung to the old hatreds, and fought as we had for generations. Until one day the sky rained fire, and a new enemy came upon us. We stand now upon the brink of destruction, for the Reign of Chaos has come at last.

Thrall, the warchief of the Orcish Horde, all along, he led his tribe live in the fringe of Lordaeron under the human control. In a downpour night, Thrall falls into sleep in a Orc hall at Arathi Highlands, at this moment he heard a voice:

“The sands of time have run out, son of Durotan. The cries of war echo upon the winds, the remnants of the past scar the land which is besieged once again by conflict. Heroes arise to challenge fate, and lead their brethren to battle. As mortal armies rush blindly towards their doom, The Burning Shadow comes to consume us all. You must rally the Horde, and lead your people to their destiny.

I will answer all of your questions in time, young warchief. For now, rally your warriors and prepare to leave this land, cross the sea to the distant land of Kalimdor. We will speak again. ”                                                                                                                            

Thrall believes the prophesy of Blood Raven Medivh. Three days later, He and Grom Hellscream's Warsong Clan meet in the Lordaeron coast to the distant lands of Kalimdor. But the Goblin Zeppelins they take encountered storms in the middle. Thrall and Grom falling to the islands, they want to find each other and then to Kalimdor.

 

For the sake of simplicity, we assume that Thrall and Grom may fall into any islands x and y, only by Thrall to find Grom or by Grom to find Thrall. Give you the map of this island, please judge that Thrall and Gtom can meet?

输入

There are multiple test case in the input file, first line is a case number T. Each test case will begin with two integers N (0 <= N < 2001) and M (0 <= M < 10001), where N is the number of islands and M is number of portal. Next M lines each line contains two integers a and b, indicated there is a portal in island a that people can go from a to b by this portal. The island numbered from 1 to N.

输出

For each test case, your output should be in one line with “Kalimdor is just ahead” (without quotes, hereinafter the same) if Thrall and Grom can meet or “The Burning Shadow consume us all” otherwise as indicated in the sample output. 

示例输入

23 21 21 33 21 22 3

示例输出

Case 1: The Burning Shadow consume us allCase 2: Kalimdor is just ahead

 

题意为给定n个点和m条有向边,问能不能任意给出两个点,至少有一条路径可以从a到b。

has[i][j],表示可以从i到达j, 对于每个点s,进行广搜,记录下has[s][ ] .

然后只要判断一下对于任意两个点都满足 has[i][j]==1||has[j][i]==1,就可以了。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#define rd(x) scanf("%d",&x)
#define rd2(x,y)  scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
const int maxn=2010;
vector<int>g[maxn];
bool has[maxn][maxn];
bool vis[maxn];
int n,m;
bool ok;void bfs(int s)
{memset(vis,0,sizeof(vis));vis[s]=1;queue<int>q;q.push(s);while(!q.empty()){int first=q.front();// cout<<"fist"<<first<<endl;q.pop();int len=g[first].size();for(int i=0;i<len;i++){int v=g[first][i];if(vis[v])continue;has[s][v]=1;q.push(v);vis[v]=1;}}
}int main()
{int t,c=1;rd(t);while(t--){ok=1;rd2(n,m);memset(has,0,sizeof(has));for(int i=0;i<maxn;i++)g[i].clear();int u,v;while(m--){rd2(u,v);g[u].push_back(v);}for(int i=1;i<=n;i++){bfs(i);}/*for(int i=1;i<=n;i++){for(int j=1;j<=n;j++)cout<<has[i][j]<<" ";cout<<endl;}*/for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){if(!has[i][j]&&!has[j][i]){ok=0;break;}}if(ok==0)break;}if(ok)printf("Case %d: Kalimdor is just ahead\n",c++);elseprintf("Case %d: The Burning Shadow consume us all\n",c++);}return 0;
}

参考网址: http://blog.csdn.net/sr_19930829/article/details/44925325







这篇关于第四届 山东省ACM B Thrall’s Dream(连通图 OR BFS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/311226

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为

POJ 3057 最大二分匹配+bfs + 二分

SampleInput35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSampleOutput321impossible

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访

双头BFS

牛客月赛100 D题,过了80%数据,调了一下午。。。烦死了。。。 还是没调试出来,别人的代码用5维的距离的更新有滞后性,要在遍历之前要去重。。。 #include<bits/stdc++.h>using namespace std;const int N=2e3+10;char g[N][N];int dis[N][N],d1[N][N];int n,m,sx,sy;int dx

Knight Moves -uva 简单的BFS遍历

昨天刚学了BFS的遍历,在uva上找了个题敲了出来,感觉还不错,最近敲代码挺有手感的,希望这种状态保持下去 #include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_SIZE 10 + 5#define LEN 100 + 10using namespace std;in

【转载】ACM感悟

今天看了一篇我们学校前辈的ACM的感悟,觉得写的十分有道理,这里转载,文章还会不断的改进和更新。 原文链接:http://www.cnblogs.com/Chierush/p/3760870.html?ADUIN=1339764596&ADSESSION=1401536826&ADTAG=CLIENT.QQ.5329_.0&ADPUBNO=26349 声明:本文是写给弱校ACM新手的一点

【CF】D. Arthur and Walls(BFS + 贪心)

D题 解题思路就是每次检查2X2的方格里是否只有一个‘*’,如果有的话这个*就需要变成‘.’,利用BFS进行遍历,入队的要求是这个点为. 一开始将所有的'.'全部加入队列,如果碰到一个'*'变成'.'就入队,判断的时候从4个方向就行判断 题目链接:http://codeforces.com/contest/525/problem/D #include<cstdio>#include<

我们依旧在追梦的路上-山东省第六届ACM比赛总结

这场比赛从结果而言达到了预期(金牌),从过程而言和我的预期相差甚远(打的太乱,个人发挥很差),还好关键时刻队友抗住压力,负责后果真的不堪设想。 热身赛 热身赛纯粹测机器的,先把A,B,C草草水过(A题小写x打成大写的也是醉了),我和老高开始各种测机器,long long不出所料是lld的,试了一下除0和数组越界的re问题,发现没有re,只有wa(甚至数组越界还AC了),至于栈深的话也没过多追