Pagini recente » Monitorul de evaluare | Diferente pentru problema/aquilla intre reviziile 2 si 1 | Diferente pentru problema/aquilla intre reviziile 4 si 3 | Diferente pentru problema/aquilla intre reviziile 3 si 2 | Cod sursa (job #3359476)
#pragma GCC optimize("Ofast")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int N = 1e3 + 5;
int n, m;
struct MaxFlow
{
int n;
queue<int> q;
vector<int> ad[N];
int p[N], d[N];
int cap[N][N]; /// cap[i][j] = cat flux putem sa pusham pe muchia i j
int sursa, destinatie;
void AddEdge(int x, int y, int c)
{
ad[x].push_back(y);
ad[y].push_back(x);
cap[x][y] = c;
}
bool BFS()
{
for (int i = 1; i <= n; i++)
d[i] = 1e9;
while (!q.empty())
q.pop();
q.push(sursa);
d[sursa] = 0;
while (!q.empty())
{
int i = q.front();
q.pop();
for (int x : ad[i])
if (cap[i][x] > 0 && d[x] > d[i] + 1)
{
d[x] = d[i] + 1;
q.push(x);
p[x] = i;
if (x == destinatie)
return true;
}
}
return d[destinatie] != 1e9;
}
int Saturate()
{
int minn = INT_MAX;
int copyDest = destinatie;
while (copyDest != sursa)
{
minn = min(minn, cap[p[copyDest]][copyDest]);
copyDest = p[copyDest];
}
copyDest = destinatie;
while (copyDest != sursa)
{
cap[p[copyDest]][copyDest] -= minn;
cap[copyDest][p[copyDest]] += minn;
copyDest = p[copyDest];
}
return minn;
}
int Solve()
{
int ans = 0;
while (BFS())
{
ans += Saturate();
}
return ans;
}
MaxFlow(int s, int d, int _n)
{
n = _n;
sursa = s;
destinatie = d;
memset(cap, 0, sizeof(cap));
}
};
signed main()
{
fin >> n >> m;
MaxFlow flow(1, n, n);
while (m--)
{
int x, y, c;
fin >> x >> y >> c;
flow.AddEdge(x, y, c);
}
fout << flow.Solve();
return 0;
}