-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_BFS.cpp
More file actions
39 lines (38 loc) · 898 Bytes
/
Copy pathgraph_BFS.cpp
File metadata and controls
39 lines (38 loc) · 898 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
vector<int> adj[6];
bool visited[6];
int distance[6];
queue<int> q;
adj[0].push_back(1);
adj[0].push_back(3);
adj[0].push_back(4);
adj[3].push_back(0);
adj[2].push_back(1);
adj[2].push_back(3);
adj[2].push_back(4);
adj[4].push_back(5);
adj[4].push_back(2);
adj[4].push_back(3);
adj[5].push_back(1);
adj[5].push_back(4);
adj[5].push_back(3);
for(int i=0;i<6;i++)visited[i]=0;
visited[5]=true;
distance[5]=0;
q.push(5);
while(!q.empty()){
int s=q.front();q.pop();
for(auto u: adj[s]){
if(visited[u]) continue;
visited[u]=true;
distance[u]=distance[s]+1;
cout<<u<<"->5:"<<distance[u]<<"\n";
q.push(u);
}
}
return 0;
}