Advertisement
dimon-torchila

Untitled

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