Pagini recente » Cod sursa (job #575052) | Cod sursa (job #1364484) | Cod sursa (job #2849995) | Cod sursa (job #2382895) | Cod sursa (job #241710)
Cod sursa(job #241710)
#include <fstream>
#include <vector>
using namespace std;
const int maxn = 1001;
const int inf = 1 << 29;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int n, m;
int a[maxn][maxn];
int f[maxn][maxn];
int cap[maxn][maxn];
int t[maxn];
int c[maxn];
int viz[maxn];
vector<int> g[maxn];
void read()
{
in >> n >> m;
int x, y, c;
for ( int i = 1; i <= m; ++i )
{
in >> x >> y >> c;
cap[x][y] += c;
g[x].push_back(y);
g[y].push_back(x);
}
}
int bf()
{
int p = 0, u = 0;
for ( int i = 1; i <= n; ++i )
viz[i] = 0;
viz[1] = 1;
c[p] = 1;
while ( p <= u )
{
int x = c[p++];
if ( x == n ) continue;
for ( int i = 0; i < g[x].size(); ++i )
{
int y = g[x][i];
if ( cap[x][y] == f[x][y] || viz[y] ) continue;
viz[y] = 1;
c[++u] = y;
t[y] = x;
}
}
return viz[n];
}
inline int min(int x, int y)
{
return x < y ? x : y;
}
int main()
{
read();
int flow = 0;
for ( int fmin = inf; bf(); )
for ( int i = 0; i < g[n].size(); ++i )
{
int x = g[n][i];
if ( f[x][n] == cap[x][n] || !viz[x] ) continue;
t[n] = x;
fmin = inf;
for ( x = n; x != 1; x = t[x] )
fmin = min(fmin, cap[ t[x] ][x] - f[ t[x] ][x]);
if ( !fmin ) continue;
for ( x = n; x != 1; x = t[x] )
{
f[ t[x] ][x] += fmin;
f[x][ t[x] ] -= fmin;
}
flow += fmin;
}
out << flow << endl;
return 0;
}