Pagini recente » Cod sursa (job #1435373) | Cod sursa (job #2790616) | Cod sursa (job #188773) | Cod sursa (job #2567432) | Cod sursa (job #3003577)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int N = 1e3 + 1;
const int INF = 1e7;
vector<int> gr[N];
int n, m, x, y, z;
int parnt[N];
int cap[N][N];
bool bfs(int s, int t)
{
memset(parnt, 0, sizeof(parnt));
parnt[s] = 1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int nod = q.front();
q.pop();
for (auto i: gr[nod])
{
if (parnt[i] == 0 && cap[nod][i] > 0)
{
parnt[i] = nod;
q.push(i);
if(i == t)
return true;
}
}
}
return 0;
}
int maxflow(int s, int t)
{
int flow = 0, minflow;
while(bfs(1, n))
{
minflow = INF;
for (auto i: gr[t])
{
int nod = i;
if (parnt[nod] == 0 || cap[nod][t] <= 0) continue;
nod = t;
while (nod != s)
{
minflow = min(minflow, cap[parnt[nod]][nod]);
nod = parnt[nod];
}
nod = t;
while (nod != s)
{
cap[parnt[nod]][nod] -= minflow;
nod = parnt[nod];
}
flow += minflow;
}
}
return flow;
}
int main()
{
fin>>n>>m;
for (int i = 1; i <= m; i++)
{
fin>>x>>y>>z;
gr[x].push_back(y);
gr[y].push_back(x);
cap[x][y] = z;
}
fout<<maxflow(1, n);
}