Pagini recente » Cod sursa (job #2892235) | Cod sursa (job #1510509) | Cod sursa (job #719422) | Cod sursa (job #136995) | Cod sursa (job #1248384)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define NN 1024
int n , m , x , y , z , i,cap[ NN ][ NN ] , flux[ NN ][ NN ], st[NN],tt[NN];
bool viz[ NN ];
struct nod{
int val;
nod *next;
};
nod *G[ NN ] , *tmp;
void add(int &x,int &y){
tmp = new nod;
tmp->val = y;
tmp->next= G[x];
G[x] = tmp;
}
int bfs(){
st[0] = st[1] = 1;
memset( viz , NULL , sizeof( viz ) );
int i ;
for( i = 1; i <= st[0] ; ++i ){
x = st[i];
if(x == n ) continue;
for(tmp = G[x] ; tmp ; tmp = tmp->next)
{
y = tmp->val;
if( cap[x][y] == flux[x][y] || viz[y] ) continue;
viz[y] = true;
st[ ++st[0] ] = y;
tt[y] = x;
}
}
return viz[n];
}
int main()
{
freopen("maxflow.in" , "r" , stdin);
freopen("maxflow.out", "w" , stdout);
scanf("%d %d",&n,&m);
while( m-- ){
scanf("%d %d %d",&x,&y,&z);
add(x,y);
add(y,x);
cap[x][y] += z;
}
int flow,fmin;
for(flow = 0; bfs() ; )
for( tmp = G[n] ; tmp ; tmp = tmp->next ){
x = tmp->val;
if( cap[x][n] == flux[x][n] || !viz[x] ) continue;
tt[n] = x;
fmin = 1<<30;
for( x = n; x != 1; x = tt[x] )
fmin = min( fmin , cap[ tt[x] ][x] - flux[ tt[x] ][x] );
if( fmin == 0 ) continue;
for( x = n; x !=1; x = tt[x] ){
flux[ tt[x] ][x] += fmin;
flux[x][ tt[x] ] -= fmin;
}
flow += fmin;
}
printf("%d\n", flow);
return 0;
}