Advertisement
Guest User

Untitled

a guest
May 14th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <list>
  5.  
  6. #define DISTANCE 6
  7.  
  8. std::vector<int> bfs (std::vector<std::list<int> > graph, int graph_size, int start)
  9. {
  10.         // Create arrays to keep distances and to mark visited nodes
  11.         std::vector<bool> visited (graph_size + 1);
  12.         std::vector<int>  distance (graph_size + 1);
  13.         // Set default values
  14.         for(int i = 0; i <= graph_size; i++)
  15.         {
  16.             distance[i] = -1;
  17.             visited[i] = false;
  18.         }
  19.        
  20.         std::queue<int> queue;
  21.        
  22.         visited[start]  = true;
  23.         distance[start] = 0;
  24.  
  25.         queue.push(start);
  26.         while (!queue.empty())
  27.         {
  28.             int current_vert = queue.front();
  29.             queue.pop();
  30.             std::list<int> lst = graph[current_vert];   // list of nodes, adjacent to current_vert
  31.             // Traverse all the adjacent nodes
  32.             for (std::list<int>::iterator it = lst.begin(); it != lst.end(); it++)
  33.             {
  34.                 int adjacent = *it;
  35.                 if(!visited[adjacent])
  36.                 {
  37.                     visited[adjacent] = true;
  38.                     distance[adjacent] = distance[current_vert] + DISTANCE;
  39.                     queue.push(adjacent);
  40.                 }
  41.             }
  42.         }
  43.         return distance;
  44. }
  45.  
  46. int main()
  47. {
  48.     int ntests = 0, vertices = 0, edges = 0;
  49.    
  50.     std::cin >> ntests;
  51.     for(int i = 0; i < ntests; i++)
  52.     {
  53.         std::cin >> vertices >> edges;
  54.         std::vector<std::list<int> > graph;
  55.         // Create adjacency list
  56.         for(int i = 0; i <= vertices; ++i)
  57.             graph.push_back(std::list<int>());
  58.        
  59.         // Insert edges into adjacency list
  60.         for(int i = 0; i < edges; ++i) {
  61.             int x, y;
  62.             std::cin >> x >> y;
  63.             graph[x].push_back(y);
  64.             graph[y].push_back(x);
  65.         }
  66.        
  67.         int start;      // start vertex
  68.         std::cin >> start;
  69.        
  70.         // Calculate vector of distances
  71.         std::vector<int> distance = bfs (graph, vertices, start);
  72.  
  73.         // Print shortest distances from start vertex to every other vertices
  74.         for(int i = 1; i <= vertices; i++) {
  75.             if (i != start) {
  76.                 std::cout << distance[i] << " ";
  77.             }
  78.         }
  79.         std::cout << std::endl;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement