-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3549.cpp
123 lines (111 loc) · 2.7 KB
/
3549.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//+base
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SIZE(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
//+dinic
template<typename CT=int>
struct DINIC {
struct edge {
int u, v;
CT c, t;
edge() {}
edge(int u, int v, CT c, CT t): u(u), v(v), c(c), t(t) {}
};
struct node : vector<int> { // index in es
bool vis; // used by BFS
int d, cur; // distance of s->i / current edge idx
node() {}
node(int N): vector<int>(N) {}
};
vector<edge> es;
vector<node> ns;
int s, t;
DINIC(int N, int s, int t): ns(N, N), s(s), t(t) {}
void add(int u, int v, CT c) {
int sz = es.size();
es.pb(edge(u, v, c, 0));
es.pb(edge(v, u, 0, 0));
ns[u][v] = sz;
ns[v][u] = sz+1;
}
bool bfs(){
for(auto&n : ns) n.vis = false;
queue<int> q;
q.push(s);
ns[s].d = 0;
ns[s].vis = true;
while(!q.empty()){
int x = q.front(); q.pop();
rep(i, 0, ns[x].size()){
auto& e = es[ ns[x][i] ];
if(!ns[e.v].vis && e.c > e.t){
ns[e.v].vis = true;
ns[e.v].d = ns[x].d + 1;
q.push(e.v);
}
}
}
return ns[t].vis;
}
CT dfs(int x, CT a) {
if(x == t || a == 0) return a;
CT flow = 0;
int& i = ns[x].cur;
while(i < ns[x].size()){
auto& e = es[ ns[x][i] ];
if(ns[x].d+1 == ns[e.v].d){
CT f = e.c - e.t;
if(f > a && a >= 0)f = a;
f = dfs(e.v, f);
if(f > 0){
e.t += f;
es[ns[x][i]^1].t -= f;
flow += f;
a -= f;
if(a == 0)break;
}
}
i += 1;
}
return flow;
}
CT run(){
CT flow = 0;
while(bfs()){
for(auto&n : ns) n.cur = 0;
flow += dfs(s, -1);
}
return flow;
}
};
//+main
int solve(){
int N, M;
cin >> N >> M;
DINIC<int> dinic(N, 0, N-1);
rep(m, 0, M){
int u, v, c;
cin >> u >> v >> c;
dinic.add(u-1, v-1, c);
}
return dinic.run();
}
int main(){
int T;
cin >> T;
cout.precision(12);
rep(t, 0, T) {
cout << "Case #" << (t+1) << ": " << solve() << endl;
}
return 0;
}