Pagini recente » Cod sursa (job #2805356) | Cod sursa (job #527278) | Istoria paginii runda/jc2015-runda1/clasament | Cod sursa (job #404456) | Cod sursa (job #932592)
Cod sursa(job #932592)
#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 () ; )
{
int nod=n;
flow=C[T[n]][n];
for( ; nod != 1 ; nod=T[nod] )
if( flow > C[T[nod]][nod] )
flow=C[T[nod]][nod];
for( nod = n ; 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;
}