Pagini recente » Cod sursa (job #1749077) | Cod sursa (job #657940) | Clasament dupa rating | Monitorul de evaluare | Cod sursa (job #1462456)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define Max_Node 1024
#define INF 110000
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
int N, M, max_flow, curent_flow;
int C[Max_Node][Max_Node], F[Max_Node][Max_Node];
int Dad[Max_Node];
bool Viz[Max_Node];
vector < int > V[Max_Node];
bool BFS()
{
queue < int > Q;
Q.push(1);
memset(Viz, 0, sizeof(Viz));
Viz[1] = true;
while (!Q.empty())
{
int nod = Q.front();
Q.pop();
if (nod == N) continue;
for (vector < int > :: iterator it = V[nod].begin(); it != V[nod].end(); it++)
{
if (C[nod][*it] == F[nod][*it] || Viz[*it]) continue;
Viz[*it] = true;
Q.push(*it);
Dad[*it] = nod;
}
}
return Viz[N];
}
int main()
{
fin >> N >> M;
for (int x, y, c, i = 1; i <= M; i++)
{
fin >> x >> y >> c;
C[x][y] = c;
V[x].push_back(y);
V[y].push_back(x);
}
while (BFS())
{
for (int i = 0; i < V[N].size(); i++)
{
int nod = V[N][i];
if (C[nod][N] == F[nod][N] || !Viz[nod]) continue;
Dad[N] = nod;
curent_flow = INF;
for (int nod = N; nod != 1; nod = Dad[nod])
curent_flow = min(curent_flow, C[Dad[nod]][nod] - F[Dad[nod]][nod]);
for (int nod = N; nod != 1; nod = Dad[nod])
{
F[Dad[nod]][nod] += curent_flow;
F[nod][Dad[nod]] -= curent_flow;
}
max_flow += curent_flow;
}
}
fout << max_flow << '\n';
fin.close();
fout.close();
return 0;
}