[Swust OJ 85]--单向公路(BFS)
题目链接:http://acm.swust.edu.cn/problem/0085/
某个地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,且有公路的并不都能双向行驶。现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一个城镇到达另一个城镇。(我们规定,城镇自己跟自己可互相到达,即A可到达A).
Input
第一行只有一个数N,下面将跟着2N行数据. 在前N行数据中,对于每行数据,最开头一个数字number,表明这一行总共有number个数,number的下一个数为i,代表编号为i的那个城镇.这行余下的就是跟i有公路连接的城镇的(编号)名单,且只能从城镇i驶向其他城镇。如 4 1 2 3,表明:此行有4个数,跟城镇1有公路连接的城镇是编号为2和3的城镇.是从1连到2 和3 ,不能从2 和3 连到1. 在后N行数据中,每行由两个数字组成a,b(表示城镇的编号). 对于每个输入的数有如下关系 0 <= input_number <= 1000 .
对于输入数据中的每个a,b,判断是否可以从城镇a通过公路到达城镇b,如果可以,输出Yes;否则输出No.
Sample Input
3
4 1 2 3
3 4 5
3 5 8
1 2
1 8
4 8
|
Yes
No
Yes
|
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int mpt[][], vis[];
int bfs(int x, int y, int n){
queue<int> Q;
Q.push(x);
while (!Q.empty()){
int now = Q.front();
Q.pop();
if (now == y) return ;
for (int i = ; i < n; i++){
if (!vis[i] && mpt[now][i]){
vis[i] = ;
Q.push(i);
}
}
}
return ;
}
int main(){
int n, t, x, y, i, j, maxn = -;
cin >> t;
for (i = ; i < t; i++){
cin >> n >> x;
for (j = ; j < n - ; j++){
cin >> y;
mpt[x][y] = ;
maxn = max(maxn, max(x, y) + );//找出出现的点的最大值+1,避免大范围搜索不必要的点
}
}
for (i = ; i < t; i++){
memset(vis, , sizeof(vis));
cin >> x >> y;
cout << (bfs(x, y, maxn) ? "Yes\n" : "No\n");
}
return ;
}
以前有个代码,个人感觉没问题,但是一直Rutime Error,有大神路过给个原因吧~~~
代码如下:
#include <stdio.h>
#include <string.h>
#define maxn 10001
int map[maxn][maxn], max;
int dfs(int star, int next)
{
int i, flag, vis[maxn];
memset(vis, , sizeof(vis));
vis[star] = ;
if (map[star][next] == )
return ;
for (i = , flag = ; i <= max&&!flag; i++)
{
if (vis[i] == && map[star][i] == )
{
vis[i] = ;
flag = dfs(i, next);
vis[i] = ;
}
}
return flag;
}
int main()
{
int i, j, m, n;
int star, next;
scanf("%d", &n);
memset(map, , sizeof(map));
for (i = ; i<maxn; i++)
map[i][i] = ;
max = ;
for (i = ; i <= n; i++)
{
scanf("%d%d", &m, &star);
if (star> max)
max = star;
for (j = ; j <= m - ; j++)
{
scanf("%d", &next);
if (next > max)
max = next;
map[star][next] = ;
}
}
for (i = ; i <= n; i++)
{
scanf("%d%d", &star, &next);
if (dfs(star, next))
printf("YES\n");
else
printf("NO\n");
}
return ;
}
[Swust OJ 85]--单向公路(BFS)的更多相关文章
- 1107: 单向公路(bfs+输入整理)(DFS也可以,而且更快)
Description 某个地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,且有公路的并不都能双向行驶.现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一 ...
- YCOJ单向公路
题目: 描述 某地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,并且有的公路并不能双向行驶.现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一个城镇到达另 ...
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)
题目链接:http://acm.swust.edu.cn/problem/409/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- 【bfs】单向公路-C++
描述 某地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,并且有的公路并不能双向行驶.现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一个城镇到达另一个城镇 ...
随机推荐
- Array.Add () and += in PowerShell
$newArray = @() $newArray.Add("Hello") If I create a new array, and using the method Add() ...
- Bootstrap dropdown 使用
同样是2种方式 参考http://www.bootcss.com/javascript.html#dropdowns JS方式调用 http://www.w3resource.com/twitter- ...
- 定位CPU高的方法
CPU占用高,最常见的原因是死循环或者类死循环的操作,如果要逐一排查代码,费时费力,可以先用工具 工具1.windbg,windows出品的牛刀一枚以管理员运行windbg,File->Atta ...
- ThinkPHP 3.1.2 查询方式的一般使用2
//select id1> and id2< 默认是and $data['id']=array(array('gt',$id1),array('lt',$id2)); // $data[' ...
- HDU 1294 Rooted Trees Problem
题目大意:求有n个节点的树有几种? 题解:http://www.cnblogs.com/keam37/p/3639294.html #include <iostream> typedef ...
- C++模板:ST算法
//初始化 void init_rmq(int n){ for(int i=0;i<n;i++)d[i][0]=a[i]; for(int j=1;(1<<j)<=n;j++) ...
- hdu 4738 (双联通求桥)
2013 ACM/ICPC Asia Regional Hangzhou Online 题目大意:有n个岛,曹操在一些岛之间建了一些桥,每个桥上有一些士兵把守,周瑜只有一个炸弹只能炸掉一个桥,炸弹需要 ...
- oracle 中的select ...connect by prior ...start with 及(+)的用法
1.select ...connect by prior ...start with的用法: select ... from <tablename> where <condition ...
- C++的一些编程规范(基于google)
1.所有头文件都应该使用#define 防止头文件被多重包含,命名格式可以参考<PROJECT>_<PATH>_<FILE>_H 2.使用前置声明尽量减少.h文件中 ...
- Why is celsius = 5 * (fahr - 32) / 9 ?
Go to my personal blog There is a program to print Fahrenheit-Celsius table as below. #include <s ...