Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include<fstream>
  5. #include <iostream>
  6. #include <queue>
  7. #include <deque>
  8. #include <vector>
  9. using namespace std;
  10.  
  11. struct vertex
  12. {
  13. vector<int> edges;
  14. bool visited;
  15. };
  16.  
  17. int dist = 0;
  18.  
  19. int BFS(vertex Graph[], int v, int target) {
  20. deque<int> Q;
  21. Q.push_front(v);
  22. Graph[v].visited = true;
  23. while (!Q.empty()) {
  24. int t = Q.back();
  25. Q.pop_back();
  26. if (t == target) {
  27. return t;
  28. }
  29. for (unsigned int i = 0; i < Graph[t].edges.size(); i++) {
  30. int u = Graph[t].edges[i];
  31. if (!Graph[u].visited) {
  32. Graph[u].visited = true;
  33. Q.push_front(u);
  34. }
  35. }
  36. }
  37. return -1;
  38. }
  39.  
  40. int main()
  41. {
  42. ifstream in("matrix.txt");
  43. int n=7;
  44. //cin >> n;
  45. vertex Graph[7];
  46. int k;
  47. //cin >> k;
  48. for (int i = 0; i < 9; i++)
  49. {
  50. int a, b;
  51. in >> a >> b;
  52. a--;
  53. b--;
  54. Graph[a].edges.push_back(b);
  55. Graph[b].edges.push_back(a);
  56. }
  57.  
  58. for (int i = 0; i < n; i++) {
  59. Graph[i].visited = false;
  60. }
  61.  
  62. int s, t;
  63. cin >> s >> t;
  64.  
  65. cout << BFS(Graph, s, t);
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement