Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <queue>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int numofgraphVertex, numofgraphEdges;
  11. queue<int> Queue;
  12.  
  13. ifstream fin ("pathbge1.in");
  14. ofstream fout ("pathbge1.out");
  15.  
  16. fin >> numofgraphVertex >> numofgraphEdges;
  17. vector<vector<int>> AdjacencyList(numofgraphVertex, vector<int>());
  18. int mindistance[numofgraphVertex];
  19.  
  20. for(int md = 0; md < numofgraphVertex; ++md)
  21. {
  22. mindistance[md] = INT32_MAX;
  23. }
  24.  
  25. int col1, col2;
  26.  
  27. for(int i = 0; i < numofgraphEdges; ++i)
  28. {
  29. fin >> col1 >> col2;
  30. AdjacencyList[col1 - 1].push_back(col2 - 1);
  31. AdjacencyList[col2 - 1].push_back(col1 - 1);
  32. }
  33.  
  34. int nodes[numofgraphVertex];
  35. Queue.push(0);
  36. mindistance[0] = 0;
  37. nodes[0] = 1;
  38. while (!Queue.empty())
  39. {
  40. int curNode = Queue.front();
  41. Queue.pop();
  42. for (int vertex = 0; vertex < AdjacencyList[curNode].size(); ++vertex)
  43. {
  44. if (mindistance[AdjacencyList[curNode][vertex]] > mindistance[curNode] + 1)
  45. {
  46. nodes[AdjacencyList[curNode][vertex]] = 1;
  47. Queue.push(AdjacencyList[curNode][vertex]);
  48. mindistance[AdjacencyList[curNode][vertex]] = mindistance[curNode] + 1;
  49. }
  50. }
  51. }
  52.  
  53. for (int i = 0; i < numofgraphVertex; ++i)
  54. {
  55. fout << mindistance[i] << ' ';
  56. }
  57.  
  58. fin.close();
  59. fout.close();
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement