Pagini recente » Monitorul de evaluare | Istoria paginii utilizator/mihanechita | Cod sursa (job #1392912) | Istoria paginii utilizator/ovidelw | Cod sursa (job #294518)
Cod sursa(job #294518)
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n, m, maxflow = 0;
int a[1001][1001];
int t[1001];
queue<int> c;
void read();
void solve();
void write();
bool bfs();
int main()
{
read();
solve();
write();
return 0;
}
bool bfs()
{
int i, nod;
memset(t, 0, sizeof(t));
c.push(1);
while (!c.empty())
{
nod = c.front();
c.pop();
for (i = 1; i <= n; ++i)
{
if (a[nod][i] && !t[i])
{
t[i] = nod;
c.push(i);
if (i == n)
{
while (!c.empty())
{
c.pop();
}
return 1;
}
}
}
}
return 0;
}
void solve()
{
int flow, i, k;
while (bfs())
{
for (k = 1; k < n; ++k)
{
if (a[k][n] && t[k])
{
flow = a[k][n];
for (i = k; i != 1; i = t[i])
{
if (flow > a[t[i]][i])
{
flow = a[t[i]][i];
}
}
for (i = k; i != 1; i = t[i])
{
a[t[i]][i] -= flow;
a[i][t[i]] += flow;
}
a[k][n] -= flow;
a[n][k] += flow;
maxflow += flow;
}
}
}
}
void read()
{
int i, x, y, z;
FILE *fin = fopen("maxflow.in", "r");
fscanf(fin, "%d%d", &n, &m);
for (i = 1; i <= m; ++i)
{
fscanf(fin, "%d%d%d", &x, &y, &z);
a[x][y] = z;
}
fclose(fin);
}
void write()
{
FILE *fout = fopen("maxflow.out", "w");
fprintf(fout, "%d\n", maxflow);
fclose(fout);
}