Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma GCC optimize("Ofast")
- //#pragma GCC optimize ("unroll-loops")
- //#pragma GCC optimize ("fast-math")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native")
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <vector>
- #include <map>
- #include <set>
- #include <list>
- #include <ctime>
- #include <stack>
- #include <queue>
- #include <iomanip>
- #include <cstdlib>
- #include <unordered_map>
- #include <unordered_set>
- #include <cstddef>
- #include <deque>
- #include <cstdio>
- #include <fstream>
- #include <random>
- #include <climits>
- #include <chrono>
- using namespace std;
- #define forn(i, j, k) for(int i = (int)(j); i < (int)(k); i++)
- #define forn1(i, j, k) for(int i = (int)(j); i <= (int)(k); i++)
- #define pb push_back
- #define mp make_pair
- #define f first
- #define s second
- #define all(x) begin(x), end(x)
- #define sz(a) ((int)((a).size()))
- //#define endl '\n'
- const long long INF = 1e18 + 1;
- const unsigned long long MOD = 1e9 + 7;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- //std::mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
- #define int ll
- int c[507][507];
- int f[507][507];
- int d[507];
- int s = 1, t = 500;
- vector<int> p;
- int bfs_create(int min_w) {
- fill(d, d + 507, INF);
- queue<int> q;
- q.push(s);
- d[s] = 0;
- while(!q.empty()) {
- int v = q.front();
- q.pop();
- for (int u = 1; u <= t; ++u) {
- if(d[u] != INF) continue;
- if (c[v][u] - f[v][u] >= min_w) {
- q.push(u);
- d[u] = d[v] + 1;
- }
- }
- }
- return d[t];
- }
- int dfs_flow(int v, int cost, int min_w) {
- if (v == t || cost == 0) {
- return cost;
- }
- for (;p[v] <= t; ++p[v]) {
- int u = p[v];
- if (d[u] == d[v] + 1 && c[v][u] - f[v][u] >= min_w) {
- int delta = dfs_flow(u, min(cost, c[v][u] - f[v][u]), min_w);
- if (delta != 0) {
- f[v][u] += delta;
- f[u][v] -= delta;
- return delta;
- }
- }
- }
- return 0;
- }
- int dinica(int min_w) {
- int max_flow = 0;
- while (bfs_create(min_w) < INF) {
- p.assign(t + 1, 0);
- int flow = dfs_flow(s, INF, min_w);
- while (flow != 0) {
- max_flow += flow;
- flow = dfs_flow(s, INF, min_w);
- }
- }
- return max_flow;
- }
- int find_max_flow() {
- int flow = 1 << 30;
- int ans = 0;
- while (flow > 0) {
- ans += dinica(flow);
- flow >>= 1;
- }
- return ans;
- }
- vector<int> flow;
- vector<vector<int>> dek;
- int cnt = 0;
- map<pair<int, int>, vector<int>> num_edges;
- vector<int> weights;
- pair<int, int> get_edge(int u, int v) {
- for (auto num : num_edges[make_pair(u, v)]) {
- if (weights[num] > 0) {
- return {weights[num], num};
- }
- }
- return {0, 0};
- } // wei, num
- int dfs(int v, int cost) {
- if (v == t) {
- return cost;
- }
- for (int u = 1; u <= t; ++u) {
- if (f[v][u] > 0) {
- auto [wei, num] = get_edge(min(u, v), max(u, v));
- int delta = dfs(u, min({cost, f[v][u], wei}));
- if (delta != 0) {
- weights[num] -= delta;
- f[v][u] -= delta;
- f[u][v] += delta;
- dek[int(flow.size())].push_back(num);
- return delta;
- }
- }
- }
- return 0;
- }
- void dekomposition() {
- int fl = dfs(1, INF);
- while (fl != 0) {
- flow.push_back(fl);
- fl = dfs(1, INF);
- }
- }
- void solve() {
- int n, m;
- cin >> n >> m;
- dek.resize(n * m + 7);
- weights.resize(m + 7);
- t = n;
- forn(i, 0, m) {
- int u, v, w;
- cin >> u >> v >> w;
- c[u][v] += w;
- weights[i + 1] = w;
- num_edges[make_pair(min(u, v), max(u, v))].push_back(i + 1);
- }
- find_max_flow();
- dekomposition();
- cout << flow.size() << endl;
- for (int i = 0; i < int(flow.size()); ++i) {
- cout << flow[i] << " " << dek[i].size() << " ";
- reverse(all(dek[i]));
- for (auto j : dek[i]) {
- cout << j << " ";
- }
- cout << endl;
- }
- }
- int32_t main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- cout.precision(30);
- int TEST_CASES;
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- TEST_CASES = 1;
- #else
- TEST_CASES = 1;
- #endif
- while (TEST_CASES--) {
- solve();
- #ifdef LOCAL
- cout << "__________________________" << endl;
- #endif
- }
- #ifdef LOCAL
- cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement