Advertisement
igoryanchik

DDD

Nov 6th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <utility>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. using vvi = vector<vector<int>>;
  8. const int inf = 1e9;
  9.  
  10. void dfs(vvi& graph, vector<int>& visited, vector<int>& vertexes, int v)
  11. {
  12.     visited[v] = 1;
  13.     vertexes.push_back(v);
  14.  
  15.     for (int i = 0; i < graph.size(); ++i)
  16.         if (graph[v][i] && !visited[i])
  17.             dfs(graph, visited, vertexes, i);
  18. }
  19.  
  20. vector<int> bfs(vvi& graph, int start)
  21. {
  22.     vector<int> visited(graph.size(), 0);
  23.     vector<int> degree(graph.size(), 0);
  24.     queue<int> q;
  25.  
  26.     visited[start] = 1;
  27.     q.push(start);
  28.  
  29.     while (!q.empty())
  30.     {
  31.         int v = q.front();
  32.         q.pop();
  33.  
  34.         for (int i = 0; i < graph.size(); ++i)
  35.         {
  36.             if (graph[v][i] && !visited[i])
  37.             {
  38.                 degree[v]++;
  39.                 visited[i] = 1;
  40.                 q.push(i);
  41.             }
  42.         }
  43.  
  44.     }
  45.  
  46.     return degree;
  47. }
  48.  
  49. vector<int> djikstra(vector<vector<int>>& graph, int start)
  50. {
  51.     vector<int> dist(graph.size(), inf);
  52.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
  53.  
  54.     dist[start] = 0;
  55.     q.emplace(dist[start], start);
  56.  
  57.     while (!q.empty())
  58.     {
  59.         pair<int, int> u = q.top();
  60.         q.pop();
  61.  
  62.         if (u.first > dist[u.second]) continue;
  63.  
  64.         for (int i = 0; i < graph[u.second].size(); ++i)
  65.         {
  66.             if (graph[u.second][i] != 0 && dist[i] > dist[u.second] + graph[u.second][i])
  67.             {
  68.                 dist[i] = dist[u.second] + graph[u.second][i];
  69.                 q.emplace(dist[i], i);
  70.             }
  71.         }
  72.     }
  73.  
  74.     return dist;
  75. }
  76.  
  77.  
  78.  
  79. int main()
  80. {
  81.  
  82.     vvi graph = { { 0, 5, 6, 6, 6, 4, 5, 0, 0, 8, 8, 4, 4 },
  83.                 { 5, 0, 8, 0, 3, 8, 4, 8, 6, 6, 6, 3, 4 },
  84.                 { 6, 8, 0, 2, 0, 0, 0, 9, 3, 5, 3, 8, 1 },
  85.                 { 6, 0, 2, 0, 2, 4, 7, 7, 7, 9, 5, 5, 5 },
  86.                 { 6, 3, 0, 2, 0, 1, 5, 5, 4, 4, 1, 4, 2 },
  87.                 { 4, 8, 0, 4, 1, 0, 8, 1, 5, 4, 5, 8, 6 },
  88.                 { 5, 4, 0, 7, 5, 8, 0, 6, 9, 0, 1, 2, 0 },
  89.                 { 0, 8, 9, 7, 5, 1, 6, 0, 4, 6, 7, 3, 3 },
  90.                 { 0, 6, 3, 7, 4, 5, 9, 4, 0, 4, 4, 1, 1 },
  91.                 { 8, 6, 5, 9, 4, 4, 0, 6, 4, 0, 9, 2, 7 },
  92.                 { 8, 6, 3, 5, 1, 5, 1, 7, 4, 9, 0, 6, 9 },
  93.                 { 4, 3, 8, 5, 4, 8, 2, 3, 1, 2, 6, 0, 3 },
  94.                 { 4, 4, 1, 5, 2, 6, 0, 3, 1, 7, 9, 3, 0 } };
  95.  
  96.  
  97.     vector<int> degree = bfs(graph, 1);
  98.     cout << degree[1];
  99.  
  100.     vector<int> visited(graph.size(), 0);
  101.     vector<int> ver;
  102.     dfs(graph, visited, ver, 0);
  103.  
  104.     cout << '\n';
  105.     for (auto it : ver)
  106.         cout << it << " ";
  107.     cout << '\n';
  108.  
  109.     vvi dist(graph.size());;
  110.  
  111.     for (int i = 0; i < graph.size(); ++i)
  112.     {
  113.         dist[i] = djikstra(graph, i);
  114.     }
  115.  
  116.     cout << '\n';
  117.     for (int i = 0; i < graph.size(); ++i)
  118.     {
  119.         for (auto it : dist[i])
  120.             cout << it << " ";
  121.         cout << '\n';
  122.     }  
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement