Pagini recente » Cod sursa (job #1912759) | Cod sursa (job #1231445) | Profil StarGold2 | Cod sursa (job #1657921) | Cod sursa (job #1046226)
#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 1000
#define mp make_pair
#define pb push_back
int n, m;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int graph[MAX_N][MAX_N];
void read(){
fin >> n >> m;
int a, b, c;
for(int i=1; i<=m; i++){
fin >> a >> b >> c;
graph[a][b] = c;
}
}
int father[MAX_N]; // vector de tati folosit in parcurgerea bf;
queue<int> coada; // coada in care adaug pe rand nodurile in parcurgerea bf
bool bf(int s, int t){ // s - pct de plecare, t-pct destinatie
bool viz[MAX_N];
memset(viz,false,sizeof(viz));
coada.push(s);
father[s] = -1; // sursa nu are tata;
viz[s] = true;
while(!coada.empty()){
int nod = coada.front();
coada.pop();
for(int i=1; i<=n; i++){
if(graph[nod][i]){
if(!viz[i]){
viz[i] = true;
father[i] = nod;
coada.push(i);
}
}
}
}
return viz[t] == true;
} // functia returneaza true daca exista drum de la s la t;
int solve(int s, int t){
int max_flow = 0;
while(bf(s,t)){ // cat timp exista drum de la s la t;
int minim = graph[father[t]][t];
for(int i=father[t]; i!=s; i=father[i]){
int new_min = graph[father[i]][i];
if(minim > new_min) minim = new_min;
}
for(int i=t; i!=s; i=father[i]){
graph[i][father[i]] += minim;
graph[father[i]][i] -= minim;
}
max_flow +=minim;
}
return max_flow;
}
int main(){
read();
fout << solve(1,n);
return 0;
}