Pagini recente » Cod sursa (job #2494611) | Cod sursa (job #950943) | Cod sursa (job #2196049) | Cod sursa (job #2123325) | Cod sursa (job #1380188)
#include <fstream>
#include <queue>
using namespace std;
#define N 1005
#define M 5005
#define INF 2147483647
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int n, m, s, d;
int c[N][N], f[N][N];
int t[N];
int flux = 0;
int lst[N], vf[2 * M], urm[2 * M], nvf = 0;
queue<int> q;
inline bool bf()
{
bool ok = 0;
for(int i = 1; i <= n; i++)
t[i] = 0;
q.push(s);
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = lst[x]; i; i = urm[i])
{
int y = vf[i];
if(!t[y] && c[x][y] > f[x][y])
{
if(y != d)
{
t[y] = x;
q.push(y);
}
else
ok = 1;
}
}
}
return ok;
}
int main()
{
in >> n >> m;
s = 1;
d = n;
for(int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
in >> c[x][y];
vf[++nvf] = y;
urm[nvf] = lst[x];
lst[x] = nvf;
vf[++nvf] = x;
urm[nvf] = lst[y];
lst[y] = nvf;
}
for(; bf();)
{
for(int i = lst[d]; i; i = urm[i])
{
int y = vf[i];
if(t[y] && c[y][d] > f[y][d])
{
t[d] = y;
int minim = INF;
for(int x = d; x != s; x = t[x])
if(minim > c[t[x]][x] - f[t[x]][x])
minim = c[t[x]][x] - f[t[x]][x];
if(minim <= 0)
continue;
flux += minim;
for(int x = d; x != s; x = t[x])
{
f[t[x]][x] += minim;
f[x][t[x]] -= minim;
}
}
}
}
out << flux << '\n';
}