Pagini recente » Cod sursa (job #3351979) | Cod sursa (job #2374550) | Cod sursa (job #3351966) | Cod sursa (job #1469552) | Cod sursa (job #3335689)
#include <bits/stdc++.h>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n, m, capacitate[1001][1001], tata[1001];
vector <int> vecini[1001];
bool bfs (int s, int t)
{
memset(tata, -1, 4 * (n + 1));
queue <int> q;
tata[s] = 0;
q.push(s);
while (!q.empty())
{
int i = q.front();
q.pop();
for (auto j : vecini[i])
if (tata[j] == -1 and capacitate[i][j] > 0)
{
tata[j] = i;
if (j == t)
return true;
q.push(j);
}
}
return false;
}
int EdmondsKarp (int s, int t)
{
int flux_maxim = 0;
while (bfs(s, t))
{
int flux_curr = INT_MAX;
for (int j = t; j != s; j = tata[j])
{
int i = tata[j];
flux_curr = min(flux_curr, capacitate[i][j]);
}
for (int j = t; j != s; j = tata[j])
{
int i = tata[j];
capacitate[i][j] -= flux_curr;
capacitate[j][i] += flux_curr;
}
flux_maxim += flux_curr;
}
return flux_maxim;
}
int main()
{
f>>n>>m;
for (int i=1; i<=m; i++)
{
int x, y, w;
f>>x>>y>>w;
vecini[x].push_back(y);
vecini[y].push_back(x);
capacitate[x][y] += w;
}
g<<EdmondsKarp(1, n);
}