[Swust OJ 85]--单向公路(BFS)

题目链接:http://acm.swust.edu.cn/problem/0085/

Time limit(ms): 5000      Memory limit(kb): 65535

Description

某个地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,且有公路的并不都能双向行驶。现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一个城镇到达另一个城镇。(我们规定,城镇自己跟自己可互相到达,即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 .

Output

对于输入数据中的每个a,b,判断是否可以从城镇a通过公路到达城镇b,如果可以,输出Yes;否则输出No.

Sample Input

1

2

3

4

5

6

7

8

3

4 1 2 3

3 4 5

3 5 8

1 2

1 8

4 8

Sample Output

1

2

3

Yes

No

Yes

解题思路:处理好数据后直接bfs~~~

代码如下:

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 int mpt[1001][1001], vis[1001];
 7 int bfs(int x, int y, int n){
 8     queue<int> Q;
 9     Q.push(x);
10     while (!Q.empty()){
11         int now = Q.front();
12         Q.pop();
13         if (now == y) return 1;
14         for (int i = 0; i < n; i++){
15             if (!vis[i] && mpt[now][i]){
16                 vis[i] = 1;
17                 Q.push(i);
18             }
19         }
20     }
21     return 0;
22 }
23 int main(){
24     int n, t, x, y, i, j, maxn = -1;
25     cin >> t;
26     for (i = 0; i < t; i++){
27         cin >> n >> x;
28         for (j = 0; j < n - 2; j++){
29             cin >> y;
30             mpt[x][y] = 1;
31             maxn = max(maxn, max(x, y) + 1);//找出出现的点的最大值+1,避免大范围搜索不必要的点
32         }
33     }
34     for (i = 0; i < t; i++){
35         memset(vis, 0, sizeof(vis));
36         cin >> x >> y;
37         cout << (bfs(x, y, maxn) ? "Yes\n" : "No\n");
38     }
39     return 0;
40 }

以前有个代码,个人感觉没问题,但是一直Rutime Error,有大神路过给个原因吧~~~

代码如下:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define maxn 10001
 4 int map[maxn][maxn], max;
 5 int dfs(int star, int next)
 6 {
 7     int i, flag, vis[maxn];
 8     memset(vis, 0, sizeof(vis));
 9     vis[star] = 1;
10     if (map[star][next] == 1)
11         return 1;
12     for (i = 1, flag = 0; i <= max&&!flag; i++)
13     {
14         if (vis[i] == 0 && map[star][i] == 1)
15         {
16             vis[i] = 1;
17             flag = dfs(i, next);
18             vis[i] = 0;
19         }
20     }
21     return flag;
22 }
23 int main()
24 {
25     int i, j, m, n;
26     int star, next;
27     scanf("%d", &n);
28     memset(map, 0, sizeof(map));
29     for (i = 0; i<maxn; i++)
30         map[i][i] = 1;
31     max = 0;
32     for (i = 1; i <= n; i++)
33     {
34         scanf("%d%d", &m, &star);
35         if (star> max)
36             max = star;
37         for (j = 1; j <= m - 2; j++)
38         {
39             scanf("%d", &next);
40             if (next > max)
41                 max = next;
42             map[star][next] = 1;
43         }
44     }
45     for (i = 1; i <= n; i++)
46     {
47         scanf("%d%d", &star, &next);
48         if (dfs(star, next))
49             printf("YES\n");
50         else
51             printf("NO\n");
52     }
53     return 0;
54 }

时间: 2024-08-30 13:32:41

[Swust OJ 85]--单向公路(BFS)的相关文章

线段树 [SWUST OJ 764] 校门外的树 Plus Plus

校门外的树 Plus Plus(0764) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 214 Accepted: 15 Description 西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米.我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置,另一端在 L 的位置:数轴上的每个整数点,即 1,2,……,L,都种有一棵树. 现在要将这排树的某一段涂成某种颜色,给定 N 组区间[ 

YCOJ单向公路

题目: 描述 某地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,并且有的公路并不能双向行驶.现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一个城镇到达另一个城镇.(我们规定,城镇自己跟自己可互相到达,即 a 可到达 a). 输入 第一行只有一个数 N ,下面将跟着 2N 行数据. 在前 N 行数据中,每行数据开头第一个数字 number,表明这一行总共有 number个数, number的下一个数为 i ,代表编号为 i 的城镇.这行余下的就是跟

swust oj 1026--Egg pain&#39;s hzf

题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf is crazy about reading math recently,and he is thinking about a boring problem. Now there are n integers Arranged in a line.For each integer,he wants to know

SWUST OJ Euclid&#39;s Game(0099)

Euclid's Game(0099) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1855 Accepted: 589 Description Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the boar

swust oj 649--NBA Finals(dp,后台略(hen)坑)

题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is

背包 [POJ 2184 SWUST OJ 145] Cow Exhibition

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 3653 Description "Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by Dana Lyons The cows want to prove to

[Swust OJ 404]--最小代价树(动态规划)

题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 以下方法称为最小代价的字母树:给定一正整数序列,例如:4,1,2,3,在不改变数的位置的条件下把它们相加,并且用括号来标记每一次加法所得到的和. 例如:((4+1)+ (2+3))=((5)+(5))=10.除去原数不4,1,2,3之外,其余都为中间结果,如5,5,10,将中间结果相加

swust oj 1126--神奇的矩阵(BFS,预处理,打表)

题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈特13成功解决了填数矩阵问题.不知道他这一周又从哪儿弄来一神奇的矩阵,于是逃课潜心研究了一周,终于发现了其中的奥秘:该矩阵有2行.4列,即8个小方块,每个小方块上有一个数字,即:1 2 3 45 6 7 8对于这个神奇的矩阵,有3种变换方式,具体如下:变换A:上下两行数字互换,如上图可变为:5 6

[Swust OJ 1023]--Escape(带点其他状态的BFS)

解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Description BH is in a maze,the maze is a matrix,he wants to escape! Input The input consists of multiple test cases. For each case,the first line contains 2 inte