-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathBFS.cpp
More file actions
62 lines (61 loc) · 1.12 KB
/
BFS.cpp
File metadata and controls
62 lines (61 loc) · 1.12 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
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define INF 0x3f3f3f
#define fi first
#define se second
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef tuple<int,int,int> iii;
vvi AdjList;
vector<bool> vstd;
void init(int vertex)
{
vstd.clear();
vstd.assign(vertex,false);
AdjList.clear();
AdjList.resize(vertex);
}
int bfs( int vertex ,int from , int arrival )
{
vi dist(vertex);
dist[from] = 0;
vstd[from] = true;
queue<int> p; p.push(from);
while( !p.empty() )
{
int front = p.front(); p.pop();
for( auto v : AdjList[front] )
if( !vstd[v] )
{
dist[v] = dist[front]+1;
vstd[v] = true;
if(v == arrival) return dist[v];
p.push(v);
}
}
return -1;
}
int main()
{
int TC;
int vertex, edges;
int from, arrival;
scanf("%d", &TC);
while(TC--)
{
scanf("%d%d", &vertex, &edges);
init(vertex);
for (int i = 0; i < edges; ++i)
{
scanf("%d%d", &from, &arrival);
AdjList[from-1].pb(arrival-1);
AdjList[arrival-1].pb(from-1);
}
printf("%d\n", bfs(vertex,0,vertex-1) );
}
return 0;
}