Pagini recente » Cod sursa (job #794029) | Cod sursa (job #613004) | Cod sursa (job #812730) | Cod sursa (job #1480839) | Cod sursa (job #1048917)
#include<iostream>
#include<fstream>
#include<cstring>
#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];
vector<int> list[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;
list[a].push_back(b);
}
}
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];
for(int i=1; i<=n; i++) viz[i] = false;
coada.push(s);
father[s] = -1; // sursa nu are tata;
viz[s] = true;
while(!coada.empty()){
int nod = coada.front();
coada.pop();
for(size_t i=0; i<list[nod].size(); i++){
int next = list[nod][i];
if(!viz[next] && graph[nod][next]){
viz[next] = true;
father[next] = nod;
coada.push(next);
}
}
}
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]){
if(graph[i][father[i]] == 0) list[i].push_back(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;
}