Advertisement
CyberN00b

m_graph

Jan 8th, 2023 (edited)
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5. class Graph{
  6. public:
  7.  
  8.  
  9.     Graph(long long size) : visited(size, false) {
  10.         for(long long i = 0; i < size; ++i) {
  11.             matrix.push_back(vector<long long>(size,0));
  12.             string s;
  13.             cout << "Enter name " << i + 1 << " vertex: ";
  14.             cin >> s;
  15.             nodes.push_back(s);
  16.             node_to_index[s] = i;
  17.         }
  18.     }
  19.  
  20.     long long first(string v) {
  21.         long long index = node_to_index[v];
  22.         for(long long i = 0; i < matrix[index].size(); ++i) {
  23.             if (matrix[index][i] != 0)
  24.                 return i;
  25.         }
  26.         return -1;
  27.     }
  28.  
  29.     long long next(string v, long long in) {
  30.         long long index = node_to_index[v];
  31.         for (long long i = in + 1; i < matrix[index].size(); ++i) {
  32.             if (matrix[index][i] != 0)
  33.                 return i;
  34.         }
  35.         return -1;
  36.     }
  37.  
  38.     string vertex(string v, int i) {
  39.         if (matrix[node_to_index[v]][i])
  40.             return nodes[i];
  41.         return "";
  42.     }
  43.  
  44.     void add_v(string v) {
  45.         node_to_index[v] = matrix.size();
  46.         nodes.push_back(v);
  47.         matrix.push_back(vector<long long>(matrix[0].size(), 0));
  48.         for(auto& x : matrix) {
  49.             if(!x.empty())
  50.                 x.push_back(0);
  51.         }
  52.     }
  53.  
  54.     void add_e(string v, string w, int c) {
  55.         matrix[node_to_index[v]][node_to_index[w]] = c;
  56.     }
  57.  
  58.     void del_v(string v) {
  59.         long long index = node_to_index[v];
  60.         matrix.erase(matrix.begin() + index);
  61.         visited.erase(visited.begin() + index);
  62.         nodes.erase(nodes.begin() + index);
  63.         node_to_index.erase(v);
  64.         for(long long i = 0; i < matrix.size(); ++i)
  65.             matrix.erase(matrix.begin() + index);
  66.         for (auto& x : node_to_index)
  67.             if (x.second > index)
  68.                 --x.second;
  69.     }
  70.  
  71.     void del_e(string v, string w) {
  72.         matrix[node_to_index[v]][node_to_index[w]] = 0;
  73.     }
  74.  
  75.     void edit_v(string v, bool mark) {
  76.         visited[node_to_index[v]] = mark;
  77.     }
  78.  
  79.     void edit_e(string v, string w, int c) {
  80.         matrix[node_to_index[v]][node_to_index[w]] = c;
  81.     }
  82.  
  83.     void print() {
  84.         for(long long i = 0; i < matrix.size(); ++i) {
  85.             for (long long j = 0; j < matrix[i].size(); ++j) {
  86.                 cout << matrix[i][j] << ' ';
  87.             }
  88.             cout << endl;
  89.         }
  90.     }
  91.  
  92.     long long get_weight(string first, string second) {
  93.         long long i = node_to_index[first], j = node_to_index[second];
  94.         if (i < matrix.size() && j < matrix.size())
  95.             return matrix[i][j];
  96.         else throw runtime_error("l");
  97.     }
  98.  
  99.     bool is_visited(string node) {
  100.         return visited[node_to_index[node]];
  101.     }
  102.  
  103.     vector<string> get_nodes() {
  104.         return nodes;
  105.     }
  106.  
  107. private:
  108.  
  109.     vector<string> nodes;
  110.     vector<bool> visited;
  111.     vector<vector<long long>> matrix;
  112.     unordered_map<string, long long> node_to_index;
  113.  
  114. };
  115.  
  116.  
  117. void dfs(Graph g, string name, vector<vector<string> >& res, long long int k, vector<string> way) {
  118.     if (g.is_visited(name) || k < 0)
  119.         return;
  120.     way.push_back(name);
  121.     if (k == 0) {
  122.         res.push_back(way);
  123.         return;
  124.     }
  125.     g.edit_v(name, true);
  126.     for (long long index = g.first(name); index != -1; index = g.next(name, index)) {
  127.         auto v = g.vertex(name, index);
  128.         dfs(g, v, res, k - g.get_weight(name, v), way);
  129.     }
  130.     g.edit_v(name, false);
  131. }
  132.  
  133. vector<vector<string> > get_ways(Graph g, long long int k) {
  134.     vector<vector<string> > res;
  135.     for (auto& x : g.get_nodes()) {
  136.         vector<string> way = {};
  137.         dfs(g, x, res, k, way);
  138.     }
  139.     return res;
  140. }
  141.  
  142. int main(){
  143.     int size;
  144.     cout << "Enter graph size: ";
  145.     cin >> size;
  146.     Graph G(size);
  147.     int cCount, n;
  148.     cout << "Enter edge count: ";
  149.     cin >> cCount;
  150.     cout << "Enter k:";
  151.     cin >> n;
  152.     for(int i = 0; i < cCount; ++i){
  153.         string first_v, second_v;
  154.         int weight;
  155.         cout << "Enter " << (i + 1) << " edge: ";
  156.         cin >> first_v >> second_v >> weight;
  157.         G.add_e(first_v, second_v, weight);
  158.     }
  159.     G.print();
  160.     cout << endl;
  161.     vector<vector<string>> ways = get_ways(G, n);
  162.     cout << "Found " << ways.size() << " ways:\n";
  163.     for (auto& x : ways) {
  164.         for (auto &y: x)
  165.             cout << y << ' ';
  166.         cout << endl;
  167.     }
  168.     return 0;
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement