Pagini recente » Cod sursa (job #2042457) | Cod sursa (job #2885578) | Cod sursa (job #1998544) | Cod sursa (job #3172530) | Cod sursa (job #469255)
Cod sursa(job #469255)
#include<fstream>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 1 << 30;
int maxflow, n, m;
int c[1001][1001], f[1001][1001], cr[1001], t[1001];
bool s[1001];
queue<int> q;
inline int abs(int x)
{
return x < 0 ? -x : x;
}
bool bfs();
void dim();
int main()
{
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
fin >> n >> m;
for (int i = 1, nod1, nod2, cap; i <= m; ++i)
{
fin >> nod1 >> nod2 >> cap;
c[nod1][nod2] = cap;
}
while (bfs())
dim();
fout << maxflow;
}
bool bfs()
{
memset(s, 0, sizeof(s));
memset(cr, 0, sizeof(cr));
memset(t, 0, sizeof(t));
cr[1] = INF;
while (!q.empty()) q.pop();
q.push(1);
while (!q.empty())
{
int i = q.front(); q.pop();
for (int j = 1; j <= n; ++j)
if (!s[j])
{
if (f[i][j] < c[i][j])
{
cr[j] = min(c[i][j] - f[i][j], cr[i]);
t[j] = i, s[j] = true;
q.push(j);
if (j == n) return true;
}
if (f[j][i] > 0)
{
cr[j] = min(f[j][i], cr[i]);
t[j] = -i, s[j] = true;
q.push(j);
if (j == n) return true;
}
}
}
return false;
}
void dim()
{
int j = n, i = n;
while (j != 1)
{
i = abs(t[j]);
if (t[j] > 0)
f[i][j] += cr[n];
else
f[j][i] -= cr[n];
j = abs(t[j]);
}
maxflow += cr[n];
}