Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. vector<bool> marks;
  10.  
  11. // Обход в глубину
  12. void dfs(int current, const vector<vector<double>>& graph)
  13. {
  14.   marks[current] = true;
  15.   for(int i = 0; i < graph[current].size(); i++)
  16.     {
  17.       if(graph[current][i] != -1 && !marks[i])
  18.       dfs(i, graph);
  19.     }
  20. }
  21.  
  22. // Обход в ширину
  23. void bfs(int start, const vector<vector<double>>& graph)
  24. {
  25.   queue<int> q;
  26.   q.push(start);
  27.  
  28.   while(!q.empty())
  29.     {
  30.       int curr = q.front();
  31.       q.pop();
  32.  
  33.       marks[curr] = true;
  34.       for(int i = 0; i < graph[curr].size(); i++)
  35.     {
  36.       if(!marks[graph[curr][i]])
  37.         q.push(graph[curr][i]);
  38.     }
  39.     }
  40. }
  41.  
  42. int main()
  43. {
  44.   ifstream in("in.txt");
  45.   ofstream out("out.txt");
  46.  
  47.   int i;
  48.   in >> i;
  49.  
  50.   vector<vector<double>> matrix(i, vector<double>(i, 0));
  51.   int a, b;
  52.   double w;
  53.  
  54.   while (in >> a >> b >> w)
  55.     {
  56.       matrix[a-1][b-1] = w;
  57.     }
  58.  
  59.   out << fixed << setprecision(1);
  60.   for (int x = 0; x < matrix.size(); x++)
  61.     {
  62.       for (int y = 0; y < matrix.size(); y++)
  63.     out << matrix[x][y] << '\t';
  64.       out << endl;
  65.     }
  66.  
  67.   marks.assign(i, false);
  68.   dfs(0, matrix);
  69.  
  70.   for (int z = 0; z < marks.size(); z++)
  71.     out << marks[z] << '\t';
  72.   out << endl;
  73.  
  74.   // Нерабочий код
  75.   // Для его работы нужно менять main()
  76.   /*
  77.   marks.assign(i, false);
  78.   bfs(0, matrix);
  79.  
  80.   for (int z = 0; z < marks.size(); z++)
  81.     out << marks[z] << '\t';
  82.   out << endl;
  83.   */  
  84.   return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement