Pagini recente » Cod sursa (job #1877456) | Cod sursa (job #1684870) | Cod sursa (job #2289571) | Cod sursa (job #2191414) | Cod sursa (job #1976831)
#include <iostream>
#include <fstream>
#include <queue>
#include <climits>
#include <string.h>
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int MAX = 1005;
int graph[MAX][MAX], father[MAX];
queue < int > Q;
vector < int > G[MAX];
int max_flow;
int n, m;
int BFS(){
bool viz[MAX];
memset(viz, 0, sizeof(viz));
Q.push(1);
viz[1] = 1;
father[1] = -1;
while(!Q.empty()){
int nod = Q.front();
Q.pop();
for(auto it: G[nod]){
if(!viz[it] && graph[nod][it] > 0){
Q.push(it);
viz[it] = 1;
father[it] = nod;
}
}
}
return viz[n];
}
void ford(){
while(BFS()){
int path = INT_MAX;
for(int it = n; it != 1; it = father[it]){
int nod = father[it];
path = min(graph[nod][it], path);
}
for(int it = n; it != 1; it = father[it]){
int nod = father[it];
graph[nod][it] -= path;
graph[it][nod] += path;
}
max_flow += path;
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; ++i){
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(y);
graph[x][y] = c;
}
ford();
fout << max_flow;
return 0;
}