#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 ;
}
BZOJ-3442: 学习小组(费用流)
最新推荐文章于 2021-04-16 00:26:47 发布