传送门:【ZOJ】2071 Technology Trader
题目分析:最大权闭合子图问题。源点向订单建边,容量为利益,汇点向组件建边,容量为成本,原图所有边变成容量无穷大的边,最后跑一遍最小割,订单利益和减去最小割容量就是最大净利润。
输出方案就从源点跑一遍dfs,能从源点到达的所有点都标记上。然后看从源点出发的边的弧尾是否被标记,被标记表示被使用,然后再看从汇点出发的点,如果被标记也表示被使用。然后就没有然后了。。。。
代码如下:
#include <map>
#include <string>
#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 = 355 ;
const int MAXQ = 355 ;
const int MAXE = 100000 ;
const int INF = 0x3f3f3f3f ;
struct Edge {
int v , n ;
type_c c , rc ;
Edge () {}
Edge ( int v , type_c c , int n ) : v ( v ) , c ( c ) , n ( n ) {}
} ;
struct Node {
char s[50] ;
int val ;
void input () {
scanf ( "%s%d" , s , &val ) ;
}
} ;
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 ;
Node A[MAXN] , B[MAXN] ;
map < string , int > mp ;
bool vis[MAXN] ;
void init () {
cntE = 0 ;
CLR ( H , -1 ) ;
}
void addedge ( int u , int v , type_c c , type_c 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 dfs ( int u ) {
vis[u] = 1 ;
for ( int i = H[u] ; ~i ; i = E[i].n )
if ( !vis[E[i].v] && E[i].c )
dfs ( E[i].v ) ;
}
void solve () {
int p ;
char buf[50] ;
int sum = 0 ;
mp.clear () ;
init () ;
scanf ( "%d" , &n ) ;
REP ( i , 0 , n ) {
A[i].input () ;
mp[A[i].s] = i ;
}
scanf ( "%d" , &m ) ;
REP ( i , 0 , m ) {
B[i].input () ;
sum += B[i].val ;
scanf ( "%d" , &p ) ;
while ( p -- ) {
scanf ( "%s" , buf ) ;
addedge ( n + i , mp[buf] , INF ) ;
}
}
s = n + m , t = s + 1 , nv = t + 1 ;
REP ( i , 0 , n )
addedge ( i , t , A[i].val ) ;
REP ( i , 0 , m )
addedge ( s , i + n , B[i].val ) ;
printf ( "%d\n" , sum - ISAP () ) ;
CLR ( vis , 0 ) ;
dfs ( s ) ;
int cnt1 = 0 , cnt2 = 0 ;
//orders
for ( int i = H[s] ; ~i ; i = E[i].n )
if ( vis[E[i].v] )
++ cnt2 ;
printf ( "%d\n" , cnt2 ) ;
for ( int i = H[s] ; ~i ; i = E[i].n )
if ( vis[E[i].v] )
printf ( "%s\n" , B[E[i].v - n].s ) ;
//components
for ( int i = H[t] ; ~i ; i = E[i].n )
if ( vis[E[i].v] )
++ cnt1 ;
printf ( "%d\n" , cnt1 ) ;
for ( int i = H[t] ; ~i ; i = E[i].n )
if ( vis[E[i].v] )
printf ( "%s\n" , A[E[i].v].s ) ;
}
} e ;
int main () {
int T ;
scanf ( "%d" , &T ) ;
while ( T -- ) {
e.solve () ;
if ( T )
printf ( "\n" ) ;
}
return 0 ;
}