Pagini recente » Cod sursa (job #2168710) | Cod sursa (job #433561) | Cod sursa (job #2656224) | Cod sursa (job #2459465) | Cod sursa (job #2123962)
#include <bits/stdc++.h>
#define NMAX 1005
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int N, M, flow;
int viz[NMAX], parinte[NMAX];
int capacity[NMAX][NMAX], flux[NMAX][NMAX];
vector <int> v[NMAX];
queue <int> q;
int BFS()
{
memset(viz, 0, sizeof(viz));
viz[1] = 1;
q.push(1);
while(!q.empty())
{
int nod = q.front();
q.pop();
if (nod == N)continue;
vector <int> :: iterator it;
for (it = v[nod].begin(); it != v[nod].end(); it++)
{
int nxt = *it;
if (!viz[nxt])
{
if (flux[nod][nxt] == capacity[nod][nxt])
continue;
q.push(nxt);
parinte[nxt] = nod;
viz[nxt] = 1;
}
}
}
return viz[N];
}
int main()
{
fin >> N >> M;
for (int i = 1; i <= M; i++)
{
int x, y, z;
fin >> x >> y >> z;
v[x].push_back(y);
v[y].push_back(x);
capacity[x][y] = z;
}
while (BFS())
{
vector <int> :: iterator it;
for (it = v[N].begin(); it != v[N].end(); it++)
{
int nxt = *it;
int nod = N;
int min_flow = INF;
parinte[N] = nxt;
while (nod != 1)
{
min_flow = min(min_flow, capacity[parinte[nod]][nod] - flux[parinte[nod]][nod]);
nod = parinte[nod];
}
nod = N;
while (nod != 1)
{
flux[parinte[nod]][nod] += min_flow;
flux[nod][parinte[nod]] -= min_flow;
nod = parinte[nod];
}
flow += min_flow;
}
}
fout << flow;
fin.close();
fout.close();
return 0;
}