Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- const int maxn = 1e5 + 10;
- vector<int> graph[maxn];
- int main() {
- int num_of_vertices;
- cin >> num_of_vertices;
- int num_of_edges;
- cin >> num_of_edges;
- for(int i = 0; i < num_of_edges; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].push_back(b); // one directioanl
- graph[b].push_back(a); // two directioanal
- }
- int S, E;
- cin >> S >> E;
- queue<int> q;
- q.push(S);
- q.push(0);
- vector<bool> visited(num_of_vertices + 1, false);
- while(!q.empty()) {
- int node = q.front();
- q.pop();
- int dist = q.front();
- q.pop();
- if(node == E) {
- cout << dist << endl;
- break;
- }
- for(int i = 0; i < (int) graph[node].size(); i++) {
- int neighbour = graph[node][i];
- if(!visited[neighbour]) {
- visited[neighbour] = true;
- q.push(neighbour);
- q.push(dist + 1);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement