虽然这道题用最小割没有做出来,但是这个板子还是很棒:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )
#define CPY( a , x ) memcpy ( a , x , sizeof a )
const int MAXN = ;
const int MAXQ = ;
const int MAXE = ;
const ll INF = ; struct Edge {
int v , n ;
ll c ;
Edge () {}
Edge ( int v , ll c , int n ) : v ( v ) , c ( c ) , n ( n ) {}
} ; struct Net {
Edge E[MAXE];
int H[MAXN] , cntE ;
int d[MAXN] , num[MAXN] , cur[MAXN] , pre[MAXN] ;
int Q[MAXQ] , head , tail ;
int s , t , nv ;
int n , m ;
ll flow ; int deg[MAXN] ; inline void init () {
cntE = ;
CLR ( H , - ) ;
int i;
for(i=; i <= n+ ; i++) H[i] = -;
} inline void addedge ( int u , int v , ll c ) {
E[cntE] = Edge ( v , c , H[u] ) ;
H[u] = cntE ++ ;
E[cntE] = Edge ( u , c , H[v] ) ;
H[v] = cntE ++ ;
} inline void rev_bfs () {
CLR ( d , - ) ;
int i;
for(i=; i <= n+ ; i++) d[i] = -;
CLR ( num , ) ;
head = tail = ;
Q[tail ++] = t ;
d[t] = ;
num[d[t]] = ;
while ( head != tail ) {
int u = Q[head ++] ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( ~d[v] )
continue ;
d[v] = d[u] + ;
num[d[v]] ++ ;
Q[tail ++] = v ;
}
}
} inline ll ISAP () {
CPY ( cur , H ) ;
rev_bfs () ;
flow = ;
int u = pre[s] = s ;
while ( d[s] < nv ) {
if ( u == t ) {
ll f = INF , pos ;
for ( int i = s ; i != t ; i = E[cur[i]].v )
if ( f > E[cur[i]].c ) {
f = E[cur[i]].c ;
pos = i ;
}
for ( int i = s ; i != t ; i = E[cur[i]].v ) {
E[cur[i]].c -= f ;
E[cur[i] ^ ].c += f ;
}
flow += f ;
u = pos ;
}
for ( int &i = cur[u] ; ~i ; i = E[i].n )
if ( E[i].c && d[u] == d[E[i].v] + )
break ;
if ( ~cur[u] ) {
pre[E[cur[u]].v] = u ;
u = E[cur[u]].v ;
} else {
if ( == ( -- num[d[u]] ) )
break ;
int mmin = nv ;
for ( int i = H[u] ; ~i ; i = E[i].n )
if ( E[i].c && mmin > d[E[i].v] ) {
cur[u] = i ;
mmin = d[E[i].v] ;
}
d[u] = mmin + ;
num[d[u]] ++ ;
u = pre[u] ;
}
}
return flow ;
} inline void solve () {
int u , v ;
ll c ;
init () ;
CLR ( deg , ) ;
s = ;
t = n ;
nv = t;
REP ( i , , n ) {
scanf ( "%d%d%lld" , &u , &v , &c ) ;
addedge ( u , v , c ) ;
++ deg[u] ;
++ deg[v] ;
}
printf ( "%lld\n" , ISAP () ) ;
}
} x ; int main () {
// while ( ~scanf ( "%d" , &x.n ) && ( x.n ) ) {
// x.m = x.n - 1;
scanf("%d",&x.n);
x.solve () ;
// } return ;
}