Advertisement
playerr17

Untitled

May 6th, 2023
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. //#pragma GCC optimize("Ofast")
  2. //#pragma GCC optimize ("unroll-loops")
  3. //#pragma GCC optimize ("fast-math")
  4. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native")
  5. #define _CRT_SECURE_NO_WARNINGS
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <string>
  9. #include <cmath>
  10. #include <vector>
  11. #include <map>
  12. #include <set>
  13. #include <list>
  14. #include <ctime>
  15. #include <stack>
  16. #include <queue>
  17. #include <iomanip>
  18. #include <cstdlib>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <cstddef>
  22. #include <deque>
  23. #include <cstdio>
  24. #include <fstream>
  25. #include <random>
  26. #include <climits>
  27. #include <chrono>
  28.  
  29. using namespace std;
  30. #define forn(i, j, k) for(int i = (int)(j); i < (int)(k); i++)
  31. #define forn1(i, j, k) for(int i = (int)(j); i <= (int)(k); i++)
  32. #define pb push_back
  33. #define mp make_pair
  34. #define f first
  35. #define s second
  36. #define all(x) begin(x), end(x)
  37. #define sz(a) ((int)((a).size()))
  38. //#define endl '\n'
  39. const long long INF = 1e18 + 1;
  40. const unsigned long long MOD = 1e9 + 7;
  41. typedef long long ll;
  42. typedef unsigned long long ull;
  43. typedef long double ld;
  44. //std::mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
  45.  
  46. #define int ll
  47.  
  48. int c[507][507];
  49. int f[507][507];
  50. int d[507];
  51.  
  52. int s = 1, t = 500;
  53.  
  54. vector<int> p;
  55.  
  56. int bfs_create(int min_w) {
  57.     fill(d, d + 507, INF);
  58.     queue<int> q;
  59.     q.push(s);
  60.     d[s] = 0;
  61.     while(!q.empty()) {
  62.         int v = q.front();
  63.         q.pop();
  64.         for (int u = 1; u <= t; ++u) {
  65.             if(d[u] != INF) continue;
  66.             if (c[v][u] - f[v][u] >= min_w) {
  67.                 q.push(u);
  68.                 d[u] = d[v] + 1;
  69.             }
  70.         }
  71.     }
  72.     return d[t];
  73. }
  74.  
  75. int dfs_flow(int v, int cost, int min_w) {
  76.     if (v == t || cost == 0) {
  77.         return cost;
  78.     }
  79.     for (;p[v] <= t; ++p[v]) {
  80.         int u = p[v];
  81.         if (d[u] == d[v] + 1 && c[v][u] - f[v][u] >= min_w) {
  82.             int delta = dfs_flow(u, min(cost, c[v][u] - f[v][u]), min_w);
  83.             if (delta != 0) {
  84.                 f[v][u] += delta;
  85.                 f[u][v] -= delta;
  86.                 return delta;
  87.             }
  88.         }
  89.     }
  90.     return 0;
  91. }
  92.  
  93. int dinica(int min_w) {
  94.     int max_flow = 0;
  95.     while (bfs_create(min_w) < INF) {
  96.         p.assign(t + 1, 0);
  97.         int flow = dfs_flow(s, INF, min_w);
  98.         while (flow != 0) {
  99.             max_flow += flow;
  100.             flow = dfs_flow(s, INF, min_w);
  101.         }
  102.     }
  103.     return max_flow;
  104. }
  105.  
  106. int find_max_flow() {
  107.     int flow = 1 << 30;
  108.     int ans = 0;
  109.     while (flow > 0) {
  110.         ans += dinica(flow);
  111.         flow >>= 1;
  112.     }
  113.     return ans;
  114. }
  115.  
  116. vector<int> flow;
  117. vector<vector<int>> dek;
  118. int cnt = 0;
  119.  
  120. map<pair<int, int>, vector<int>> num_edges;
  121. vector<int> weights;
  122.  
  123. pair<int, int> get_edge(int u, int v) {
  124.     for (auto num : num_edges[make_pair(u, v)]) {
  125.         if (weights[num] > 0) {
  126.             return {weights[num], num};
  127.         }
  128.     }
  129.     return {0, 0};
  130. } // wei, num
  131.  
  132. int dfs(int v, int cost) {
  133.     if (v == t) {
  134.         return cost;
  135.     }
  136.     for (int u = 1; u <= t; ++u) {
  137.         if (f[v][u] > 0) {
  138.             auto [wei, num] = get_edge(min(u, v), max(u, v));
  139.             int delta = dfs(u, min({cost, f[v][u], wei}));
  140.             if (delta != 0) {
  141.                 weights[num] -= delta;
  142.                 f[v][u] -= delta;
  143.                 f[u][v] += delta;
  144.                 dek[int(flow.size())].push_back(num);
  145.                 return delta;
  146.             }
  147.         }
  148.     }
  149.     return 0;
  150. }
  151.  
  152. void dekomposition() {
  153.     int fl = dfs(1, INF);
  154.     while (fl != 0) {
  155.         flow.push_back(fl);
  156.         fl = dfs(1, INF);
  157.     }
  158. }
  159.  
  160. void solve() {
  161.     int n, m;
  162.     cin >> n >> m;
  163.     dek.resize(n * m + 7);
  164.     weights.resize(m + 7);
  165.     t = n;
  166.     forn(i, 0, m) {
  167.         int u, v, w;
  168.         cin >> u >> v >> w;
  169.         c[u][v] += w;
  170.         weights[i + 1] = w;
  171.         num_edges[make_pair(min(u, v), max(u, v))].push_back(i + 1);
  172.     }
  173.     find_max_flow();
  174.  
  175.     dekomposition();
  176.  
  177.     cout << flow.size() << endl;
  178.     for (int i = 0; i < int(flow.size()); ++i) {
  179.         cout << flow[i] << " " << dek[i].size() << " ";
  180.         reverse(all(dek[i]));
  181.         for (auto j : dek[i]) {
  182.             cout << j << " ";
  183.         }
  184.         cout << endl;
  185.     }
  186. }
  187.  
  188. int32_t main() {
  189.     ios_base::sync_with_stdio(false);
  190.     cin.tie(NULL);
  191.     cout.tie(NULL);
  192.     cout.precision(30);
  193.     int TEST_CASES;
  194. #ifdef LOCAL
  195.     freopen("input.txt", "r", stdin);
  196.     //freopen("output.txt", "w", stdout);
  197.     TEST_CASES = 1;
  198. #else
  199.     TEST_CASES = 1;
  200. #endif
  201.     while (TEST_CASES--) {
  202.         solve();
  203. #ifdef LOCAL
  204.         cout << "__________________________" << endl;
  205. #endif
  206.     }
  207. #ifdef LOCAL
  208.     cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
  209. #endif
  210.     return 0;
  211. }
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement