-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2644.cpp
More file actions
39 lines (34 loc) · 730 Bytes
/
Copy path2644.cpp
File metadata and controls
39 lines (34 loc) · 730 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 <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> graph[101];
queue<int> q;
int visited[101];
int n,s,e,m;
void BFS(){
q.push(s);
visited[s]=1;
while(!q.empty()){
int v = q.front();
q.pop();
for(int i=0; i<graph[v].size();i++){
if (visited[graph[v][i]]==0 || visited[graph[v][i]]>visited[v]+1){
q.push(graph[v][i]);
visited[graph[v][i]]=visited[v]+1;
}
}
}
}
int main(){
cin >> n;
cin >> s>>e;
cin >> m;
for(int i =0; i<m; i++){
int x,y; cin >> x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
BFS();
cout << visited[e] - 1;
}