传送门:【HDU】4971 A simple brute force problem.
题目分析:这场多校竟然有两个网络流!我的眼睛都笑弯了~~
本题是最大权闭合子图问题,由于可能两个技术问题是相互依赖的,那么我们强连通一边缩点好了,新的花费就是强连通里的点的花费之和,然后将图按照缩点后的点建边,所有u依赖v的建边(u,v,INF),然后源点向项目建边,容量为利益,缩点后的技术问题向汇点建边,容量为缩点后的技术问题的花费。跑一遍最大流,项目利益之和减去流量就是要求的答案(净收益)。
比赛的时候17分钟就有人过了!吓死我了,差点就以为这题有比网络流更简单的方法了。。。事实是我不知道有没有更好的方法。。。暂且献上我的网络流代码好了。。。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std ;
#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REV( 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 = 100 ;
const int MAXE = 10000 ;
const int INF = 0x3f3f3f3f ;
struct Edge {
int v , c , n ;
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] , num[MAXN] , pre[MAXN] ;
int Q[MAXN] , head , tail ;
int s , t , nv ;
int flow ;
void init () {
cntE = 0 ;
CLR ( H , -1 ) ;
}
void addedge ( int u , int v , int c ) {
E[cntE] = Edge ( v , c , H[u] ) ;
H[u] = cntE ++ ;
E[cntE] = Edge ( u , 0 , H[v] ) ;
H[v] = cntE ++ ;
}
void rev_bfs () {
CLR ( d , -1 ) ;
CLR ( num , 0 ) ;
head = tail = 0 ;
d[t] = 0 ;
num[d[t]] = 1 ;
Q[tail ++] = 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] + 1 ;
Q[tail ++] = v ;
num[d[v]] ++ ;
}
}
}
int ISAP () {
rev_bfs () ;
CPY ( cur , H ) ;
flow = 0 ;
int u = pre[s] = s , i , pos , minv ;
while ( d[s] < nv ) {
if ( u == t ) {
int 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 ;
}
flow += f ;
u = pos ;
}
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 ( minv = nv , i = H[u] ; ~i ; i = E[i].n ) {
if ( E[i].c && minv > d[E[i].v] ) {
minv = d[E[i].v] ;
cur[u] = i ;
}
}
d[u] = minv + 1 ;
num[d[u]] ++ ;
u = pre[u] ;
}
}
return flow ;
}
} S ;
struct Tarjan {
Edge E[MAXE] ;
int H[MAXN] , cntE ;
int dfn[MAXN] , low[MAXN] , scc[MAXN] , scc_cnt ;
int S[MAXN] , top , dfs_clock ;
void init () {
cntE = top = scc_cnt = dfs_clock = 0 ;
CLR ( H , -1 ) ;
CLR ( scc , 0 ) ;
CLR ( dfn , 0 ) ;
}
void addedge ( int u , int v , int c ) {
E[cntE] = Edge ( v , c , H[u] ) ;
H[u] = cntE ++ ;
}
void tarjan ( int u ) {
dfn[u] = low[u] = ++ dfs_clock ;
S[top ++] = u ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( !dfn[v] ) {
tarjan ( v ) ;
low[u] = min ( low[u] , low[v] ) ;
} else if ( !scc[v] ) low[u] = min ( low[u] , dfn[v] ) ;
}
if ( low[u] == dfn[u] ) {
++ scc_cnt ;
do {
scc[S[-- top]] = scc_cnt ;
} while ( u != S[top] ) ;
}
}
} T ;
int n , m ;
int G[MAXN][MAXN] ;
int profit[MAXN] , cost[MAXN] ;
int newcost[MAXN] ;
void scanf ( int& x , char c = 0 ) {
while ( ( c = getchar () ) < '0' || c > '9' ) ;
x = c - '0' ;
while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}
void solve () {
int sum = 0 , x ;
CLR ( newcost , 0 ) ;
S.init () ;
T.init () ;
scanf ( n ) ;
scanf ( m ) ;
REP ( i , 0 , n ) {
scanf ( profit[i] ) ;
sum += profit[i] ;
};
REP ( i , 0 , m ) {
scanf ( cost[i] ) ;
}
REP ( i , 0 , n ) {
scanf ( G[i][0] ) ;
FOR ( j , 1 , G[i][0] ) {
scanf ( G[i][j] ) ;
}
}
REP ( i , 0 , m ) {
REP ( j , 0 , m ) {
scanf ( x ) ;
if ( x ) {
T.addedge ( i , j , 0 ) ;
}
}
}
REP ( i , 0 , m ) {
if ( !T.dfn[i] ) {
T.tarjan ( i ) ;
}
}
REP ( i , 0 , m ) {
newcost[T.scc[i]] += cost[i] ;
}
S.s = n + T.scc_cnt + 1 ;
S.t = S.s + 1 ;
S.nv = S.t + 1 ;
REP ( i , 0 , n ) {
S.addedge ( S.s , i , profit[i] ) ;
}
FOR ( i , 1 , T.scc_cnt ) {
S.addedge ( n + i , S.t , newcost[i] ) ;
}
REP ( i , 0 , n ) {
FOR ( j , 1 , G[i][0] ) {
S.addedge ( i , n + T.scc[G[i][j]] , INF ) ;
}
}
REP ( u , 0 , m ) {
for ( int i = T.H[u] ; ~i ; i = T.E[i].n ) {
int v = T.E[i].v ;
if ( T.scc[u] != T.scc[v] ) {
S.addedge ( n + T.scc[u] , n + T.scc[v] , INF ) ;
}
}
}
printf ( "%d\n" , sum - S.ISAP () ) ;
}
int main () {
int T , cas = 0 ;
scanf ( T ) ;
while ( T -- ) {
printf ( "Case #%d: " , ++ cas ) ;
solve () ;
}
return 0 ;
}