Pagini recente » Cod sursa (job #991734) | Cod sursa (job #2293520) | Cod sursa (job #472363) | Cod sursa (job #680435) | Cod sursa (job #932593)
Cod sursa(job #932593)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream in ("maxflow.in");
ofstream out ("maxflow.out");
const int N = 1001;
vector<int> G[N];
int n,m,C[N][N],T[N],SOL;
queue<int> Q;
void READ ()
{
in>>n>>m;
for( int x,y,c ; m ; --m )
{
in>>x>>y;
in>>C[x][y];
G[x].push_back(y);
G[y].push_back(x);
}
}
int BFS ()
{
memset(T,0,sizeof(T));
T[1]=1;
for( Q.push(1) ; Q.size() ; Q.pop() )
{
int nod = Q.front();
for( vector<int>::iterator it=G[nod].begin() ; it < G[nod].end() ; ++it)
if( C[nod][*it] && !T[*it] )
{
T[*it]=nod;
Q.push(*it);
}
}
return T[n];
}
void SOLVE ()
{
for( int flow ; BFS () ; )
for( vector<int>::iterator it=G[n].begin() ; it < G[n].end() ; ++it )
if( C[*it][n] && T[*it] )
{
flow=C[*it][n];
for( int nod= *it ; nod != 1 ; nod=T[nod] )
if( flow > C[T[nod]][nod] )
flow = C[T[nod]][nod];
C[*it][n]-=flow;
C[n][*it]+=flow;
for( int nod= *it ; nod != 1 ; nod=T[nod] )
{
C[T[nod]][nod]-=flow;
C[nod][T[nod]]+=flow;
}
SOL+=flow;
}
}
void PRINT ()
{
out<<SOL;
}
int main ()
{
READ ();
SOLVE ();
PRINT ();
return 0;
}