Pagini recente » Cod sursa (job #1914399) | Cod sursa (job #888497) | Cod sursa (job #2254611) | Cod sursa (job #2638823) | Cod sursa (job #2701703)
#include <iostream>
#include <vector>
#include <cstring>
#include <fstream>
#include <queue>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int INF = 1e9 + 9;
const int dim = 1005;
vector <int> vec[dim];
int parent[dim],viz[dim],c[dim][dim],n,m;
int BFS(int sursa,int destinatie)
{
memset(viz,0,sizeof(viz));
queue<pair<int,int> > q;
q.push({sursa, INF});
viz[sursa] = 1;
while (!q.empty())
{
pair<int,int> x = q.front();
int flow = x.second;
q.pop();
for (int j=0; j<vec[x.first].size(); j++)
{
int y = vec[x.first][j];
if (!viz[y] && c[x.first][y] > 0)
{
viz[y] = 1;
flow = min(flow, c[x.first][y]);
parent[y] = x.first;
q.push({y, flow});
if (y == destinatie) return flow;
}
}
}
return 0;
}
int Get_Flux(int sursa,int destinatie)
{
int maxflow = 0;
int flow = 0;
int ok;
while ((flow = BFS(sursa, destinatie)))
{
maxflow += flow;
int x = destinatie;
while (x != sursa)
{
int p = parent[x];
c[parent[x]][x] -= flow;
c[x][parent[x]] += flow;
x = parent[x];
}
}
return maxflow;
}
int main()
{
in >> n >> m;
for (int i=1; i<=m; i++)
{
int x,y,f;
in >> x >> y >> f;
vec[x].push_back(y);
vec[y].push_back(x);
c[x][y] = f;
}
out << Get_Flux(1,n);
return 0;
}