Pagini recente » Cod sursa (job #2644238) | Autentificare | Cod sursa (job #1323462) | Cod sursa (job #2920459) | Cod sursa (job #2955341)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int mx = 1020;
const long long inf = 1e18;
long long capacity[mx][mx];
int n,m;
vector<int> g[mx];
void read(){
in>>n>>m;
int a,b,c;
for(int i=0;i<m;i++){
in>>a>>b>>c;
g[a].push_back(b);
g[b].push_back(a);
capacity[a][b]+=c;
}
}
int flow(){
int s = 1, t = n;
vector<int> prev(n+1,-1);
queue<int> q;
q.push(s);
while(!q.empty()){
int here = q.front();
q.pop();
if(here == t){
long long flow = inf;
int now = here;
while(now != s){
flow = min(flow, capacity[prev[now]][now]);
now = prev[now];
}
now = here;
while(now != s){
capacity[prev[now]][now] -= flow;
capacity[now][prev[now]] += flow;
now = prev[now];
}
return flow;
}
for(int k:g[here]){
if(prev[k]!=-1 || capacity[here][k]==0)
continue;
prev[k] = here;
q.push(k);
}
}
return 0;
}
void solve(){
long long total_flow = 0, current_flow = 0;
do{
current_flow = flow();
total_flow+=current_flow;
}
while(current_flow);
out<<total_flow;
}
int main(){
read();
solve();
return 0;
}