Pagini recente » Cod sursa (job #1149671) | Cod sursa (job #2557731) | Cod sursa (job #2422460) | Cod sursa (job #2448391) | Cod sursa (job #1659928)
#include <bits/stdc++.h>
using namespace std;
int n , m , x , y , c , pos ;
int *adj ;
size_t tmp;
struct cell{
short v;
int c , next;
};
cell *ct;
#define s 1
int main(void)
{
freopen("maxflow.in" , "r" , stdin);
scanf("%d %d",&n,&m);
int t_adj[(const int)(n+1)];
tmp = sizeof( t_adj );
memset(t_adj , 0 , tmp);
adj = t_adj;
ct = new cell[(2*n+3)];
memset( ct , 0 , sizeof(ct) );
//ct[1]
pos = 1;
for(int i = 1; i <= m ;++i){
scanf("%d %d %d",&x,&y,&c);
++pos;
ct[pos] = { y , c , adj[x] };
adj[x] = pos;
++pos;
ct[pos] = { x , 0 , adj[y] };
adj[y] = pos;
}
int flow =0 , new_flow;
int q[(const int)(n+5)],
p[(const int)(n+5)],
e[(const int)(n+5)];
tmp = sizeof(p);
memset(e,0,tmp);
int other ,_min;
do{
new_flow = 0;
int qstart = 0 , qend = 0 ;
q[0] = 1;
memset(p,0,tmp);
p[1] = 1;
while( qstart <= qend && !p[n] ){
x = q[qstart];
for(pos = adj[x] ; pos ; pos = ct[pos].next){
y = ct[pos].v;
if( !p[y] && ct[pos].c ){
p[y] = x;
e[y] = pos;
q[++qend] = y;
}
}
++qstart;
}
_min = 0;
for(pos=adj[n]; pos; pos = ct[pos].next){
y = ct[pos].v;
other = pos ^ 1;
if( ct[other].c && p[y] ){
p[n] = y;
e[n] = other;
_min = 1<<27;
c = n;
while( c != s){
_min = min(_min,ct[e[c]].c);
c = p[c];
}
c = n;
while( c != 1 ){
ct[e[c] ].c -= _min;
ct[e[c]^1].c += _min;
c = p[c];
}
new_flow += _min;
}
}
flow += new_flow;
}while( new_flow );
freopen("maxflow.out" , "w" , stdout);
printf("%d",flow);
return 0;
}