Advertisement
he_obviously

Untitled

May 6th, 2022
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <queue>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. struct Edge {
  10.     int from = 0, to = 0, c = 0;
  11.     Edge() = default;
  12.     Edge(int a, int b, int c_) {
  13.         from = a;
  14.         to = b;
  15.         c = c_;
  16.     }
  17. };
  18.  
  19. int INF = 1000000001;
  20. int n, m;
  21. vector<vector<int>> ans;
  22. vector<Edge> edges;
  23. vector<int> ind[501];
  24. vector<int> path;
  25. vector<bool> used;
  26.  
  27. int dfs(int v, int f) {
  28.     if (f <= 0) {
  29.         return 0;
  30.     }
  31.     if (v == n) {
  32.         path.push_back(f);
  33.         ans.push_back(path);
  34.         return f;
  35.     }
  36.     used[v] = true;
  37.     for (int e : ind[v]) {
  38.         if (edges[e].c > 0 && !used[edges[e].to]) {
  39.             path.push_back(e >> 1);
  40.             int tempf = dfs(edges[e].to, min(f, edges[e].c));
  41.             if (tempf) {
  42.                 edges[e].c -= tempf;
  43.                 edges[e ^ 1].c += tempf;
  44.                 return tempf;
  45.             }
  46.             path.pop_back();
  47.         }
  48.     }
  49.     return 0;
  50. }
  51.  
  52. void find_decomposition() {
  53.     int curf = dfs(1, INF);
  54.     while (curf > 0) {
  55.         path.clear();
  56.         fill(used.begin(), used.end(), false);
  57.         curf = dfs(1, INF);
  58.     }
  59.     cout << ans.size() << "\n";
  60.     for (auto & an : ans) {
  61.         cout << an.back() << " " << an.size() - 1 << " ";
  62.         for (int j = 0; j < an.size() - 1; ++j) {
  63.             cout << an[j] + 1 << " ";
  64.         }
  65.         cout << "\n";
  66.     }
  67. }
  68.  
  69. int main() {
  70.  
  71.     cin >> n >> m;
  72.     used.resize(n + 1);
  73.  
  74.     for (int i = 0; i < m; ++i) {
  75.         int from, to, cap;
  76.         cin >> from >> to >> cap;
  77.         edges.emplace_back(from, to, cap);
  78.         ind[from].push_back(i << 1);
  79.         edges.emplace_back(to, from, -cap);
  80.         ind[to].push_back((i << 1) + 1);
  81.     }
  82.  
  83.     find_decomposition();
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement