Pagini recente » Monitorul de evaluare | Cod sursa (job #2580250) | Cod sursa (job #1308164) | Cod sursa (job #1954622) | Cod sursa (job #2886515)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, flow[N][N], cap[N][N], tata[N];
bool viz[N];
vector <int> g[N];
queue <int> q;
void element_nou(int node)
{
viz[node] = true;
if (node == n)
return ;
for (auto nxt : g[node])
{
if (!viz[nxt] && flow[node][nxt] < cap[node][nxt])
{
q.push(nxt);
tata[nxt] = node;
}
}
}
bool bfs()
{
fill(viz + 1, viz + N + 1, 0);
q.push(1);
tata[1] = 0;
while (!q.empty())
{
int x = q.front();
q.pop();
element_nou(x);
}
return viz[n];
}
void Edmonds_Karp(int &flowMax)
{
while (bfs() != 0)
{
for (auto& nod : g[n])
{
if (!viz[nod] || flow[nod][n] == cap[nod][n])
continue;
int curent = INT_MAX;
tata[n] = nod;
for (int i = n; tata[i]; i = tata[i])
{
curent = min(curent, cap[tata[i]][i] - flow[tata[i]][i]);
}
if (!curent)
continue;
for (int i = n; tata[i]; i = tata[i])
{
flow[tata[i]][i] += curent;
flow[i][tata[i]] -= curent;
}
flowMax += curent;
}
}
}
int main()
{
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
cin >> n >> m;
int x, y, c;
for (int i = 1; i <= m; ++i)
{
cin >> x >> y >> c;
cap[x][y] = c;
g[x].push_back(y);
g[y].push_back(x);
}
int flowMax = 0;
Edmonds_Karp(flowMax);
cout << flowMax;
return 0;
}