【COGS】1595 [USACO FEB05]神秘的挤奶机 二分最大流

来源:互联网 发布:js 手机号验证 编辑:程序博客网 时间:2024/04/30 17:24

传送门:【COGS】1595 [USACO FEB05]神秘的挤奶机


题目分析:赤果果的二分最大流。。


代码如下:


#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>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 )typedef long long LL ;typedef int type_c ;typedef int type_f ;const int MAXN = 205 ;const int MAXQ = 205 ;const int MAXM = 40005 ;const int MAXE = 200005 ;const int INF = 0x3f3f3f3f ;struct Edge {int v , n ;type_c c ;Edge () {}Edge ( int v , int c , int n ) : v ( v ) , c ( c ) , n ( n ) {}} ;struct Net {Edge E[MAXE] ;int H[MAXN] , cntE ;int d[MAXN] , cur[MAXN] , pre[MAXN] , num[MAXN] ;int Q[MAXQ] , head , tail ;int s , t , nv ;type_f flow ;int n , m , k ;int uu[MAXM] , vv[MAXM] , cc[MAXM] ;void clear () {cntE = 0 ;CLR ( H , -1 ) ;}void addedge ( int u , int v , int c , int rc = 0 ) {E[cntE] = Edge ( v , c , H[u] ) ;H[u] = cntE ++ ;E[cntE] = Edge ( u , rc , H[v] ) ;H[v] = cntE ++ ;}void rev_bfs () {CLR ( d , -1 ) ;CLR ( num , 0 ) ;head = tail = 0 ;Q[tail ++] = t ;d[t] = 0 ;num[d[t]] = 1 ;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] == -1 ) {Q[tail ++] = v ;d[v] = d[u] + 1 ;num[d[v]] ++ ;}}}}type_f ISAP () {CPY ( cur , H ) ;rev_bfs () ;flow = 0 ;int u = pre[s] = s , i , pos , mmin ;while ( d[s] < nv ) {if ( u == t ) {type_f f = INF ;for ( i = s ; i != t ; i = E[cur[i]].v ) if ( f > E[cur[i]].c ) {f = E[cur[i]].c ;pos = i ;}for ( i = s ; i != t ; i = E[cur[i]].v ) {E[cur[i]].c -= f ;E[cur[i] ^ 1].c += f ;}u = pos ;flow += f ;}for ( i = cur[u] ; ~i ; i = E[i].n ) if ( E[i].c && d[u] == d[E[i].v] + 1 ) break ;if ( ~i ) {cur[u] = i ;pre[E[i].v] = u ;u = E[i].v ;}else {if ( 0 == -- num[d[u]] ) break ;for ( mmin = nv , i = H[u] ; ~i ; i = E[i].n ) if ( E[i].c && mmin > d[E[i].v] ) {mmin = d[E[i].v] ;cur[u] = i ;}d[u] = mmin + 1 ;num[d[u]] ++ ;u = pre[u] ;}}return flow ;}void solve () {int l = INF , r = 0 ;scanf ( "%d%d%d" , &n , &m , &k ) ;s = 0 , t = n , nv = t + 1 ;REP ( i , 0 , m ) {scanf ( "%d%d%d" , &uu[i] , &vv[i] , &cc[i] ) ;l = min ( l , cc[i] ) ;r = max ( r , cc[i] ) ;}while ( l < r ) {int limit = ( l + r ) >> 1 ;clear () ;REP ( i , 0 , m ) if ( cc[i] <= limit ) addedge ( uu[i] , vv[i] , 1 , 1 ) ;addedge ( s , 1 , k ) ;ISAP () ;if ( flow == k ) r = limit ;else l = limit + 1 ;}printf ( "%d\n" , l ) ;}} e ;int main () {freopen ( "secretmilking.in" , "r" , stdin ) ;freopen ( "secretmilking.out" , "w" , stdout ) ;e.solve () ;return 0 ;}


0 0
原创粉丝点击