-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxFlow.cpp
More file actions
85 lines (83 loc) · 1.65 KB
/
MaxFlow.cpp
File metadata and controls
85 lines (83 loc) · 1.65 KB
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define oo 999999999999999999
int n,m,parent[10005];
ll flow[10005][10005];
struct edge
{
int d;
ll c;
edge(int a,ll b)
{
d =a;
c = b;
}
};
vector<edge> graph[10005];
ll bfs(int s,int t)
{
bool visit[n];
ll neck[n];
memset(visit,0,sizeof(visit));
//fill_n(neck,INT_MAX,n);
neck[s] = oo;
parent[s] = s;
queue<int> q;
visit[s] =true;
q.push(s);
while(!q.empty())
{
int u= q.front();
q.pop();
for(int v=0;v<n;v++)
{
//int v = graph[u][j];
if(!visit[v]&&flow[u][v]>0)
{
neck[v] = min(neck[u],flow[u][v]);
visit[v] = true;
q.push(v);
parent[v] = u;
if(v==t)
return neck[v];
}
}
}
return 0;
}
ll edm()
{
ll rf,ans(0);
while((rf=bfs(0,n-1))>0)
{
ans+=rf;
for(int i=n-1,j=parent[i];i!=j;i=j,j=parent[j])
{
flow[j][i]-=rf;
flow[i][j]+=rf;
}
}
return ans;
}
int main()
{
//freopen("in.txt", "rt", stdin);
//freopen("out.txt", "w+t", stdout);
int t,u,v;
ll c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(flow,0,sizeof(flow));
for(int i=0;i<m;i++)
{
scanf("%d%d%lld",&u,&v,&c);
//graph[u].push_back(edge(v,c));
flow[u][v] = c;
}
cout<<edm()<<endl;
}
return 0;
}