-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphBFS.c
More file actions
71 lines (56 loc) · 1.26 KB
/
Copy pathgraphBFS.c
File metadata and controls
71 lines (56 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#define INF 99999999
struct note
{
int x;
int s;
};
int main()
{
struct note que[2501];
int e[51][51] = {0},book[51]={0};
int head,tail;
// 利用flag变量来跳出两层循环
int i,j,n,m,a,b,cur,start,end,flag=0;
scanf("%d %d %d %d", &n, &m, &start, &end);
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
if(i == j) e[i][j] = 0;
else e[i][j] = INF;
for(i=1; i<=m; i++)
{
scanf("%d %d", &a, &b);
e[a][b] = 1;
e[b][a] = 1;
}
head = 1;
tail = 1;
que[tail].x = start;
que[tail].s = 0;
tail++;
book[start] = 1;
while(head < tail)
{
cur = que[head].x;
for(i=1; i<=n; i++)
{
if(book[i] == 0 && e[cur][i] == 1)
{
book[i] = 1;
que[tail].x = i;
que[tail].s = que[head].s + 1;
tail++;
}
if(que[tail-1].x == end)
{
flag = 1;
break;
}
}
if(flag == 1)
break;
head++;
}
printf("Min change point is: %d", que[tail-1].s);
return 0;
}