Max Cost Flow Snip
Max Cost Flow Snip
h>
// #include "atcoder/convolution.hpp"
// #include "atcoder/dsu.hpp"
// #include "atcoder/fenwicktree.hpp"
// #include "atcoder/lazysegtree.hpp"
// #include "atcoder/math.hpp"
#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <vector>
#include <vector>
namespace atcoder {
namespace internal {
} // namespace internal
} // namespace atcoder
namespace atcoder {
struct edge {
int from, to;
Cap cap, flow;
};
edge get_edge(int i) {
int m = int(pos.size());
assert(0 <= i && i < m);
auto _e = g[pos[i].first][pos[i].second];
auto _re = g[_e.to][_e.rev];
return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
}
std::vector<edge> edges() {
int m = int(pos.size());
std::vector<edge> result;
for (int i = 0; i < m; i++) {
result.push_back(get_edge(i));
}
return result;
}
void change_edge(int i, Cap new_cap, Cap new_flow) {
int m = int(pos.size());
assert(0 <= i && i < m);
assert(0 <= new_flow && new_flow <= new_cap);
auto& _e = g[pos[i].first][pos[i].second];
auto& _re = g[_e.to][_e.rev];
_e.cap = new_cap - new_flow;
_re.cap = new_flow;
}
Cap flow = 0;
while (flow < flow_limit) {
bfs();
if (level[t] == -1) break;
std::fill(iter.begin(), iter.end(), 0);
Cap f = dfs(dfs, t, flow_limit - flow);
if (!f) break;
flow += f;
}
return flow;
}
std::vector<bool> min_cut(int s) {
std::vector<bool> visited(_n);
internal::simple_queue<int> que;
que.push(s);
while (!que.empty()) {
int p = que.front();
que.pop();
visited[p] = true;
for (auto e : g[p]) {
if (e.cap && !visited[e.to]) {
visited[e.to] = true;
que.push(e.to);
}
}
}
return visited;
}
private:
int _n;
struct _edge {
int to, rev;
Cap cap;
};
std::vector<std::pair<int, int>> pos;
std::vector<std::vector<_edge>> g;
};
} // namespace atcoder
// #include "atcoder/mincostflow.hpp"
// #include "atcoder/modint.hpp"
// #include "atcoder/scc.hpp"
// #include "atcoder/segtree.hpp"
// #include "atcoder/string.hpp"
// #include "atcoder/twosat.hpp"
/* //SNIPPET LIST
// 1)segtree
-(used to build a segment tree and to solve range based queries and point
update)
2)freqidblocks
-(in a vector we can find number of increasing and decreasing subsegment)
3)UNIV -(gives unique vector no duplicates)
4)levaltraversalTree
5)bpow
6)sttoi
7)commonsubstring2chr
8)factorstore-(stores all the factors of a num)
9)issubstring
10)perfectSqorNot
11)seivestore
12)precompfac -(factorial)
13)range overlap
22) kth Segment tree ---> Its simply ordered set implementation via segment
trees and used for questions of type Queue Construction in leetcode
28) LcmOfArray-----> Gives the lcm of the array using primes fact method o(nlogn)
vpl movmt1={{0,1},{1,0},{-1,0},{0,-1}};
vpl movmt2={{0,1},{1,0},{-1,0},{0,-1},{1,-1},{-1,-1},{1,1},{-1,1}};
vector<mint> fact(MAXN),inv_fact(MAXN);
void precompute_factorials() {
fact[0] = 1;
for (ll i = 1; i < MAXN; i++) {
fact[i] = fact[i - 1] * i;
}
inv_fact[MAXN - 1] = fact[MAXN - 1].inv(); // Use modll's inv() for modular
inverse
for (ll i = MAXN - 2; i >= 0; i--) {
inv_fact[i] = inv_fact[i + 1] * (i + 1);
}
}
mint nCr(ll n, ll r) {
if (r > n || r < 0) return 0;
return fact[n] * inv_fact[r] * inv_fact[n - r];
}
mint nPr(ll n, ll r) {
if (r > n || r < 0) return 0;
return fact[n] * inv_fact[n - r];
}*/
void madara(){
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("Error.txt", "w", stderr);
#endif
ll ashish;
cin>>ashish;
while(ashish--){
madara();
}
return 0;
}