Pagini recente » Cod sursa (job #2823238) | Cod sursa (job #598464) | Cod sursa (job #2031930) | Cod sursa (job #2758029) | Cod sursa (job #1069247)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int inf= 1<<30;
const int nmax= 1000;
int n, m;
vector <int> v[nmax+1];
int c[nmax+1][nmax+1], f[nmax+1][nmax+1], p[nmax+1];
bool u[nmax+1];
int bfs( ) {
queue <int> q;
q.push(1);
for ( int i= 0; i<=nmax; ++i ) {
u[i]= 0;
}
u[1]= 1;
while ( !q.empty() )
{
int x= q.front();
if ( x!=n ) {
for ( int i= 0; i<(int)v[x].size(); ++i ) {
int y= v[x][i];
if ( c[x][y]!=f[x][y] && u[y]==0 ) {
u[y]= 1;
q.push(y);
p[y]= x;
}
}
}
q.pop();
}
return u[n];
}
int main()
{
fin>>n>>m;
for ( ; m>0; --m )
{
int x, y, z;
fin>>x>>y>>z;
v[x].push_back(y);
v[y].push_back(x);
c[x][y]= z;
}
int flow= 0;
while ( bfs() ) {
for ( int i= 0; i<(int)v[n].size(); ++i ) {
int x = v[n][i];
if ( f[x][n]!=c[x][n] && u[x]==1 ) {
p[n]= x;
int fmin= inf;
for ( x= n; x!=1; x= p[x] ) {
if ( c[p[x]][x]-f[p[x]][x]<fmin ) {
fmin= c[p[x]][x]-f[p[x]][x];
}
}
for ( x= n; x!= 1; x= p[x] ) {
f[p[x]][x]+= fmin;
f[x][p[x]]-= fmin;
}
flow+= fmin;
}
}
}
fout<<flow<<"\n";
return 0;
}