Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 10989 - Bomb, Divide and Conquer
Source : UVA Online Judge
Category : Graph Theory
Algorithm : Gomory Hu Tree, All pair maximum flow
Verdict : Accepted
- #include "bits/stdc++.h"
- #include "ext/pb_ds/assoc_container.hpp"
- #include "ext/pb_ds/tree_policy.hpp"
- #include "ext/rope"
-
- using namespace std;
- using namespace __gnu_pbds;
- using namespace __gnu_cxx;
-
- #define FI freopen("in.txt", "r", stdin)
- #define FO freopen("out.txt", "w", stdout)
- #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
-
- #define FOR(i, n) for (int i = 1; i <= n; i++)
- #define For(i, n) for (int i = 0; i < n; i++)
- #define ROF(i, n) for (int i = n; i >= 1; i--)
- #define Rof(i, n) for (int i = n-1; i >= 0; i--)
- #define FORI(i, n) for (auto i : n)
- #define FORAB(i, a, b) for (int i = a; i <= b; i++)
-
- #define ll long long
- #define ull unsigned long long
- #define vi vector <int>
- #define vl vector <ll>
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define mk make_pair
- #define ff first
- #define ss second
- #define eb emplace_back
- #define em emplace
- #define pb push_back
- #define ppb pop_back
- #define All(a) a.begin(), a.end()
- #define memo(a, b) memset(a, b, sizeof a)
- #define Sort(a) sort(All(a))
- #define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
- #define rev(a) reverse(All(a))
- #define sz(a) (int)a.size()
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- #define maxAll(a) *max_element(All(a))
- #define minAll(a) *min_element(All(a))
- #define allUpper(a) transform(All(a), a.begin(), :: toupper)
- #define allLower(a) transform(All(a), a.begin(), :: tolower)
- #define endl '\n'
- #define nl puts("")
- #define ub upper_bound
- #define lb lower_bound
- #define Exp exp(1.0)
- #define PIE 2*acos(0.0)
- #define Sin(a) sin(((a)*PIE)/180.0)
- #define EPS 1e-9
-
- template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
-
-
-
-
-
-
-
-
-
-
- #define here cerr << "Here" << endl;
- #define trace1(x) cerr << #x << ": " << x << endl;
- #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
- #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
- #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
- #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
- #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
-
- inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
- inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
- inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
- inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
-
- #define ones(mask) __builtin_popcount(mask) // count set bit
- #define onesLL(mask) __builtin_popcountll(mask) // for long long
- #define lzeros(mask) __builtin_clz(mask) // no of leading zeros
- #define tzeros(mask) __builtin_ctz(mask) // no of trailing zeros
-
- inline int read() { int a; scanf("%d", &a); return a; }
- inline ll readLL() { ll a; scanf("%lld", &a); return a; }
- inline double readDD() { double a; scanf("%lf", &a); return a; }
-
- template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
- int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
- ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
-
- #define inf 123456678890
- #define mod 1000000007
-
- static const int maxn = 200 + 5;
- static const int logn = 18;
-
- struct Node
- {
- int v, w;
- Node(int v = 0, int w = 0) : v(v), w(w) {}
- };
-
- vector <int> graph[maxn];
- int tnode, tedge;
- ll cap[maxn][maxn], flow[maxn][maxn];
- int level[maxn], ptr[maxn];
- int father[maxn];
- ll maxflow[maxn];
-
- bool bfs(int source, int sink)
- {
- fill(begin(level), end(level), -1);
- queue <int> PQ;
- PQ.push(source);
- level[source] = 0;
- while (!PQ.empty())
- {
- int u = PQ.front(); PQ.pop();
- for (int v : graph[u])
- {
- if (level[v] != -1 || flow[u][v] >= cap[u][v]) continue;
- level[v] = level[u] + 1;
- PQ.push(v);
- }
- }
- return level[sink] != -1;
- }
-
- ll dfs(int u, ll minCap, int sink)
- {
- if (u == sink) return minCap;
- for (int &i = ptr[u]; i < (int)graph[u].size(); i++)
- {
- int v = graph[u][i];
- if (level[v] == level[u]+1 && flow[u][v] < cap[u][v])
- {
- ll minFlow = dfs(v, min(minCap, cap[u][v] - flow[u][v]), sink);
- if (minFlow > 0)
- {
- flow[u][v] += minFlow;
- flow[v][u] -= minFlow;
- return minFlow;
- }
- }
- }
- return 0;
- }
-
- ll DinicMaxFlow(int source, int sink)
- {
- int maxFlow = 0;
- while(bfs(source, sink))
- {
- memo(ptr, 0);
- while(ll bottleneck = dfs(source, inf, sink))
- {
- maxFlow += bottleneck;
- }
- }
- return maxFlow;
- }
-
- void clearFlow()
- {
- FOR(i, tnode) FOR(j, tnode) flow[i][j] = 0;
- }
-
- ll GomoryTree()
- {
- ll ans = inf;
- FOR(i, tnode) father[i] = 1;
- FORAB(i, 2, tnode)
- {
- clearFlow();
- maxflow[i] = DinicMaxFlow(i, father[i]);
- bfs(i, father[i]);
- FORAB(j, i+1, tnode)
- {
- if (level[j] != -1 && father[j] == father[i]) father[j] = i;
- }
- if (maxflow[i] < ans) ans = maxflow[i];
- }
- return ans;
- }
-
- void clean()
- {
- FOR(i, tnode)
- {
- graph[i].clear();
- FOR(j, tnode) cap[i][j] = 0;
- }
- }
-
- int main()
- {
- FI;
-
- int tc = read();
- FOR(tcase, tc)
- {
- tnode = read();
- tedge = read();
- For(e, tedge)
- {
- int u = read();
- int v = read();
- int w = read();
- graph[u].eb(v);
- graph[v].eb(u);
- cap[u][v] += w;
- cap[v][u] += w;
- }
- bfs(1, tnode);
- bool notConnected = false;
- FOR(i, tnode)
- {
- if (level[i] == -1)
- {
- notConnected = true;
- break;
- }
- }
- if (notConnected)
- {
- printf("Case #%d: 0\n", tcase);
- clean();
- continue;
- }
- ll ans = GomoryTree();
- printf("Case #%d: %lld\n", tcase, ans);
- clean();
-
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.