Pagini recente » Cod sursa (job #2230046) | Cod sursa (job #504629) | Cod sursa (job #1494766) | Cod sursa (job #1400527) | Cod sursa (job #1627344)
#include <fstream>
#include <queue>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
#define N 1001
#define M 1001
#define INF 2147483647
int n, m, s = 1, d, flux = 0;
int c[N][N], f[N][N], t[N];
int lst[N], vf[2 * M], urm[2 * M], nvf;
queue<int> q;
inline void citeste()
{
in >> n >> m;
while(m--)
{
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;
}
return;
}
inline bool bfs()
{
bool ok = 0;
for(int x = 1; x <= n; x++)
t[x] = 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] && f[x][y] < c[x][y])
{
if(y != d)
{
t[y] = x;
q.push(y);
}
else
ok = 1;
}
}
}
return ok;
}
inline void fluxmaxim()
{
for(; bfs(); )
{
for(int i = lst[d]; i; i = urm[i])
{
int y = vf[i];
if(t[y] && f[y][d] < c[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;
}
}
}
}
return;
}
inline void afisare()
{
out << flux << '\n';
return;
}
int main()
{
citeste();
d = n;
fluxmaxim();
afisare();
return 0;
}