-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathDownload Speed.cpp
62 lines (54 loc) · 1.2 KB
/
Download Speed.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pil;
const int maxN = 501;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int N, M, p[maxN];
ll cap[maxN][maxN];
vector<int> G[maxN];
ll bfs(int s = 1, int t = N){
fill(p+1, p+N+1, -1);
p[s] = -2;
queue<pil> Q;
Q.push({s, INF});
while(!Q.empty()){
int u = Q.front().first;
ll f = Q.front().second;
Q.pop();
for(int v : G[u]){
if(p[v] == -1 && cap[u][v]){
p[v] = u;
ll aug = min(f, cap[u][v]);
if(v == t) return aug;
Q.push({v, aug});
}
}
}
return 0;
}
ll maxflow(int s = 1, int t = N){
ll flow = 0, aug = 0;
while(aug = bfs()){
flow += aug;
int u = t;
while(u != s){
int v = p[u];
cap[v][u] -= aug;
cap[u][v] += aug;
u = v;
}
}
return flow;
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a, b; i < M; i++){
ll c;
scanf("%d %d %lld", &a, &b, &c);
G[a].push_back(b);
G[b].push_back(a);
cap[a][b] += c;
}
printf("%lld\n", maxflow());
}