include iostream include string include vector include iomanip include

  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
123
124
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
typedef long long ll;
const int N = 110, M = N*(N-1);
const ll INF = 1e18;
int n, m;
int cap[2*M];
int dest[2*M];
int link[2*M];
int head[N];
int last[N];
int cnt = 0;
int s, t;
void add_arc(int i, int j, int c) {
cap[cnt] = c;
dest[cnt] = j;
link[cnt] = -1;
if(head[i] == -1)
head[i] = cnt;
else {
int arc = last[i];
link[arc] = cnt;
}
last[i] = cnt;
cnt++;
cap[cnt] = 0;
dest[cnt] = i;
link[cnt] = -1;
if(head[j] == -1) head[j] = cnt;
else {
int arc = last[j];
link[arc] = cnt;
}
last[j] = cnt;
cnt++;
}
int level[N];
int q[N];
bool bfs() {
int qh = 0, qt = 0;
fill(level, level + N, -1);
level[s] = 0;
q[qt++] = s;
while(qt > qh) {
int v = q[qh++];
for(int arc = head[v]; arc != -1; arc = link[arc]) {
int u = dest[arc], c = cap[arc];
if(level[u] == -1 && c > 0) {
q[qt++] = u;
level[u] = level[v] + 1;
}
}
}
return level[t] != -1;
}
inline ll dfs(int v = s, ll flow = INF) {
if(!flow) return 0LL;
if(v == t) return flow;
for(int arc = head[v]; arc != -1; arc = link[arc]) {
int u = dest[arc], c = cap[arc];
if(level[u] == level[v] + 1) {
ll pushed = dfs(u, min(flow, (ll)c));
if(pushed) {
cap[arc] -= pushed;
cap[arc^1] += pushed;
return pushed;
}
}
}
return 0;
}
void init() {
fill(head, head + N, -1);
}
int main() {
ios_base::sync_with_stdio(false);
// freopen("in.txt", "r", stdin);
init();
cin >>n >>m;
s = 0, t = n - 1;
for(int i = 0; i < m; i++) {
int a, b, c;
cin >>a >>b >>c;
a--, b--;
add_arc(a, b, c);
add_arc(b, a, c);
}
ll pushed = 0;
while(bfs()) {
pushed += dfs();
}
cout <<pushed <<endl;
return 0;
}