题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2521
挺神奇的一个最小割模型,如果要使得该边一定在MST上,那么要保证该边连接的两个连通块之间不存在其他边权小于等于它的边,那么自然就最小割啦。
代码:
#include
#include
#include
using namespace std ;
#define maxn 1010
#define maxv 1010
#define maxm 1010
const int inf = 0x7fffffff ;
struct network {
struct edge {
edge *next , *pair ;
int t , f ;
} *head[ maxv ] ;
network( ) {
memset( head , 0 , sizeof( head ) ) ;
}
void Add( int s , int t , int f ) {
edge *p = new( edge ) ;
p -> t = t , p -> f = f , p -> next = head[ s ] ;
head[ s ] = p ;
}
void AddEdge( int s , int t , int f ) {
Add( s , t , f ) , Add( t , s , 0 ) ;
head[ s ] -> pair = head[ t ] , head[ t ] -> pair = head[ s ] ;
}
int h[ maxv ] , gap[ maxv ] , S , T ;
edge *d[ maxv ] ;
int sap( int v , int flow ) {
if ( v == T ) return flow ;
int rec = 0 ;
for ( edge *p = d[ v ] ; p ; p = p -> next ) {
if ( p -> f && h[ v ] == h[ p -> t ] + 1 ) {
int ret = sap( p -> t , min( flow - rec , p -> f ) ) ;
p -> f -= ret , p -> pair -> f += ret , d[ v ] = p ;
if ( ( rec += ret ) == flow ) return flow ;
}
}
if ( ! ( -- gap[ h[ v ] ] ) ) h[ S ] = T ;
gap[ ++ h[ v ] ] ++ , d[ v ] = head[ v ] ;
return rec ;
}
int maxflow( ) {
for ( int i = 0 ; i ++ < T ; ) {
h[ i ] = gap[ i ] = 0 , d[ i ] = head[ i ] ;
}
gap[ 0 ] = T ;
int flow = 0 ;
for ( ; h[ S ] < T ; flow += sap( S , inf ) ) ;
return flow ;
}
} net ;
int n , m , lab , E[ maxm ][ 3 ] ;
int main( ) {
scanf( "%d%d%d" , &n , &m , &lab ) ;
for ( int i = 0 ; i ++ < m ; ) {
scanf( "%d%d%d" , &E[ i ][ 0 ] , &E[ i ][ 1 ] , &E[ i ][ 2 ] ) ;
}
net.S = n + 1 , net.T = n + 2 ;
net.AddEdge( net.S , E[ lab ][ 0 ] , inf ) ;
net.AddEdge( E[ lab ][ 1 ] , net.T , inf ) ;
for ( int i = 0 ; i ++ < m ; ) if ( i != lab && E[ i ][ 2 ] < E[ lab ][ 2 ] + 1 ) {
int flow = E[ lab ][ 2 ] + 1 - E[ i ][ 2 ] ;
net.AddEdge( E[ i ][ 0 ] , E[ i ][ 1 ] , flow ) ;
net.AddEdge( E[ i ][ 1 ] , E[ i ][ 0 ] , flow ) ;
}
printf( "%d\n" , net.maxflow( ) ) ;
return 0 ;
}