-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBFSShortestPath.cpp
81 lines (70 loc) · 1.61 KB
/
BFSShortestPath.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Refer to the BFS Graph Searching algorithm in CSE100 Slides */
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Graph {
vector<vector<int>> adj;
vector<int> dists;
public:
Graph(int n);
inline void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> & shortest_reach(int start);
};
Graph::Graph(int n) {
adj = vector<vector<int>>(n, vector<int>());
dists = vector<int>(n, -1);
}
vector<int> & Graph::shortest_reach(int start) {
int next, neighbor;
queue<int> explore;
dists[start] = 0;
explore.push(start);
while(!explore.empty()) {
next = explore.front();
explore.pop();
for(auto it = adj[next].begin(); it != adj[next].end(); ++it) {
neighbor = *it;
if(dists[neighbor] == -1) {
dists[neighbor] = dists[next] + 6;
explore.push(neighbor);
}
}
}
return dists;
}
int main() {
int queries;
cin >> queries;
for (int t = 0; t < queries; t++) {
int n, m;
cin >> n;
// Create a graph of size n where each edge weight is 6:
Graph graph(n);
cin >> m;
// read and set edges
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
// add each edge to the graph
graph.add_edge(u, v);
}
int startId;
cin >> startId;
startId--;
// Find shortest reach from node s
vector<int> distances = graph.shortest_reach(startId);
for (int i = 0; i < distances.size(); i++) {
if (i != startId) {
cout << distances[i] << " ";
}
}
cout << endl;
}
return 0;
}