-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbfs.cpp
47 lines (43 loc) · 803 Bytes
/
bfs.cpp
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
# include <fstream>
# include <vector>
# include <queue>
# include <cstring>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector <int> a[100010];
queue <int> q;
int i,j,n,m,x,y,start,cost[100010];
void bfs (int start)
{
int i;
memset(cost,-1,sizeof(cost));
cost[start]=0;
q.push(start);
while (! q.empty())
{
x=q.front (); q.pop ();
for (i=0; i<a[x].size(); ++i)
{
if (cost[a[x][i]]==-1)
{
cost[a[x][i]]=cost[x]+1;
q.push(a[x][i]);
}
}
}
}
int main ()
{
f>>n>>m>>start;
for (i=1; i<=m; ++i)
{
f>>x>>y;
a[x].push_back(y);
}
bfs(start);
for (i=1; i<=n; ++i)
g<<cost[i]<<" ";
g<<"\n";
return 0;
}