JZOJ5004. Fortress

题目大意

m 个敌人,n门炮,每门炮有一个可以攻击到的敌人的集合,可以从中选出一个敌人攻击,使得敌人的耐久-1。
初始时所有敌人的耐久都为5。敌人造成的伤害如下表:
伤害
求最坏情况下,敌人最多能造成多少伤害。

Data Constraint
n,m100

题解

和WC2016挑战NPC的思想类似,考虑将问题转化为最大匹配问题。
将一个敌人拆成7个点,如图:
图
如果有一个炮可以打到这个敌人,就向外面五个点连边。现在可以发现,当外面有1个已经被匹配时,剩余的点有3个匹配;2个被匹配时,剩余2个匹配;3个被匹配时,剩余2个匹配;4个被匹配时,剩余1个;5个被匹配时,剩余0个。刚好与表格相吻合。
所以构出图来,跑一遍带花树求最大匹配即可。
最后答案就是最大匹配-n。

时间复杂度: O(n3)

SRC

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std ;

#define N 1000 + 10
#define M 400000 + 10

int vis[N] , fa[N] ;
int Node[M] , Next[M] , Head[N] , tot = 0 ;
int Pair[N] , Ord[N] , Pre[N] , D[N] ;
int Case , n , m , ans ;
int l , r ;

void link( int u , int v ) {
    Node[++tot] = v ;
    Next[tot] = Head[u] ;
    Head[u] = tot ;
}

int Get( int x ) { return fa[x] == x ? x : fa[x] = Get(fa[x]) ; }

int LCA( int x , int y ) {
    x = Get(x) , y = Get(y) ;
    vis[0] ++ ;
    while ( 1 ) {
        if ( x ) {
            if ( vis[x] == vis[0] ) return x ;
            vis[x] = vis[0] ;
            x = Get(Pre[Pair[x]]) ;
        }
        swap( x , y ) ;
    }
}

void Merge( int u , int v , int F ) {
    while ( Get(u) != F ) {
        Pre[u] = v ;
        if ( fa[u] == u ) fa[u] = F ;
        v = Pair[u] ;
        if ( Ord[v] == 1 ) {
            D[++r] = v ;
            Ord[v] = 0 ;
        }
        if ( fa[v] == v ) fa[v] = F ;
        u = Pre[v] ;
    }
}

int Blossom( int st ) {
    for (int i = 1 ; i <= n ; i ++ ) fa[i] = i , Ord[i] = -1 ;
    l = 0 , r = 1 ;
    D[1] = st ;
    Ord[st] = 0 ;
    while ( l < r ) {
        l ++ ;
        int now = D[l] ;
        for (int p = Head[now] ; p ; p = Next[p] ) {
            int nex = Node[p] ;
            if ( Ord[nex] < 0 ) {
                Ord[nex] = 1 ;
                Pre[nex] = now ;
                if ( !Pair[nex] ) {
                    int a = now , b = nex ;
                    while ( a ) {
                        int t = Pair[a] ;
                        Pair[a] = b ;
                        Pair[b] = a ;
                        b = t ;
                        a = Pre[b] ;
                    }
                    return 1 ;
                }
                D[++r] = Pair[nex] ;
                Ord[Pair[nex]] = 0 ;
            } else {
                if ( Ord[nex] == 0 && Get(now) != Get(nex) ) {
                    int L = LCA( now , nex ) ;
                    Merge( now , nex , L ) ;
                    Merge( nex , now , L ) ;
                }
            }
        }
    }
    return 0 ;
}

int main() {
    freopen( "fortress.in" , "r" , stdin ) ;
    freopen( "fortress.out" , "w" , stdout ) ;
    scanf( "%d" ,  &Case) ;
    while ( Case -- ) {
        tot = ans = 0 ;
        memset( Head , 0 , sizeof(Head) ) ;
        memset( vis , 0 , sizeof(vis) ) ;
        memset( Pair , 0 , sizeof(Pair) ) ;
        scanf( "%d%d" , &n , &m ) ;
        for (int i = 1 ; i <= m ; i ++ ) {
            int now = 7 * (i - 1) + 1 ;
            for (int j = now ; j <= now + 3 ; j ++ )
                link( j , j + 1 ) , link( j + 1 , j ) ;
            link( now + 4 , now ) , link( now , now + 4 ) ;
            for (int j = now ; j <= now + 4 ; j ++ ) {
                link( now + 5 , j ) , link( j , now + 5 ) ;
                link( now + 6 , j ) , link( j , now + 6 ) ;
            }
        }
        for (int i = 1 ; i <= n ; i ++ ) {
            int k , now = i + 7 * m ;
            scanf( "%d" , &k ) ;
            for (int j = 1 ; j <= k ; j ++ ) {
                int x ;
                scanf( "%d" , &x ) ;
                for (int j = 7 * (x - 1) + 1 ; j <= 7 * (x - 1) + 5 ; j ++ )
                    link( now , j ) , link( j , now ) ;
            }
        }
        ans -= n ;
        n += 7 * m ;
        for (int i = 1 ; i <= n ; i ++ ) {
            if ( Pair[i] ) continue ;
            ans += Blossom(i) ;
        }
        printf( "%d\n" , ans ) ;
    }
    return 0 ;
}

以上.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值