BZOJ-3442: 学习小组(费用流)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <deque>
  
using namespace std ;
  
#define inf 0x7fffffff
#define MAXN 210
#define pf push_front
#define pb push_back
struct edge {
    edge *next , *pair ;
    int t , f , c ;
} *head[ MAXN ] ;
void Add( int s , int t , int f , int c ) {
    edge *p = new( edge ) ;
    p -> t = t , p -> f = f , p -> c = c , p -> next = head[ s ] ;
    head[ s ] = p ;
}
void AddEdge( int s , int t , int f , int c ) {
    Add( s , t , f , c ) , Add( t , s , 0 , - c ) ;
    head[ s ] -> pair = head[ t ] , head[ t ] -> pair = head[ s ] ;
}
int n , m , k , c[ MAXN ] , F[ MAXN ] , S , T , V , cost = 0 ;
char w[ MAXN ][ MAXN ] ;
int dist[ MAXN ] , slack[ MAXN ] ;
bool f[ MAXN ] ;
int aug( int v , int flow ) {
    if ( v == T ) {
        cost += flow * ( dist[ S ] - dist[ T ] ) ;
        return flow ;
    }
    f[ v ] = false ;
    int rec = 0 ;
    for ( edge *p = head[ v ] ; p ; p = p -> next ) if ( p -> f && dist[ v ] == dist[ p -> t ] + p -> c && f[ p -> t ] ) {
        int ret = aug( p -> t , min( flow - rec , p -> f ) ) ;
        p -> f -= ret , p -> pair -> f += ret ;
        if ( ( rec += ret ) == flow ) return flow ;
    } else if ( p -> f ) slack[ p -> t ] = min( slack[ p -> t ] , dist[ p -> t ] - dist[ v ] + p -> c ) ;
    return rec ;
}
bool relabel(  ) {
    int delta = inf ;
    for ( int i = 0 ; i ++ < V ; ) if ( f[ i ] ) delta = min( delta , slack[ i ] ) ;
    if ( delta == inf ) return false ;
    for ( int i = 0 ; i ++ < V ; ) if ( ! f[ i ] ) dist[ i ] += delta ;
    return true ;
}
deque < int > q ;
void Spfa(  ) {
    memset( f , false , sizeof( f ) ) ;
    for ( int i = 0 ; i ++ < V ; ) dist[ i ] = inf ;
    q.clear(  ) ;
    q.pf( T ) , dist[ T ] = 0 , f[ T ] = true ;
    while ( ! q.empty(  ) ) {
        int v = q.front(  ) ;
        q.pop_front(  ) , f[ v ] = false ;
        for ( edge *p = head[ v ] ; p ; p = p -> next ) {
            if ( ! p -> f && dist[ p -> t ] > dist[ v ] - p -> c ) {
                dist[ p -> t ] = dist[ v ] - p -> c ;
                if ( ! f[ p -> t ] ) {
                    f[ p -> t ] = true ;
                    if ( ! q.empty(  ) && dist[ p -> t ] < dist[ q.front(  ) ] ) {
                        q.pf( p -> t ) ;
                    } q.pb( p -> t ) ;
                }
            }
        }
    }
}
int costflow(  ) {
    Spfa(  ) ;
    do {
        do {
            for ( int i = 0 ; i ++ < V ; ) slack[ i ] = inf ;
            memset( f , true , sizeof( f ) ) ;
        } while ( aug( S , inf ) ) ;
    } while ( relabel(  ) ) ;
    return cost ;
}
int main(  ) {
    scanf( "%d%d%d" , &n , &m , &k ) ;
    S = n + m + 1 , T = V = n + m + 2 ;
    memset( head , 0 , sizeof( head ) ) ;
    for ( int i = 0 ; i ++ < m ; ) scanf( "%d" , c + i ) ;
    for ( int i = 0 ; i ++ < m ; ) scanf( "%d" , F + i ) ;
    for ( int i = 0 ; i ++ < n ; ) scanf( "%s" , w[ i ] + 1 ) ;
    for ( int i = 0 ; i ++ < n ; ) AddEdge( S , i , k , 0 ) ;
    for ( int i = 0 ; i ++ < n ; ) {
        if ( k - 1 ) AddEdge( i , T , k - 1 , 0 ) ;
        for ( int j = 0 ; j ++ < m ; ) if ( w[ i ][ j ] == '1' ) AddEdge( i , n + j , 1 , - F[ j ] ) ;
    }
    for ( int i = 0 ; i ++ < m ; ) for ( int j = 0 ; j ++ < n ; ) AddEdge( n + i , T , 1 , c[ i ] * ( j * j - ( j - 1 ) * ( j - 1 ) ) ) ;
    printf( "%d\n" , costflow(  ) ) ;
    return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值