【HDU】5383 Yu-Gi-Oh!【费用流】

传送门:【HDU】5383 Yu-Gi-Oh!

my  code:

#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define clr( a , x ) memset ( a , x , sizeof a )

const int MAXN = 305 ;
const int MAXE = 1000005 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
    int v , c , w , n ;
    Edge () {}
    Edge ( int v , int c , int w , int n ) : v ( v ) , c ( c ) , w ( w ) , n ( n ) {}
} ;

Edge E[MAXE] ;
int H[MAXN] , cntE ;
int d[MAXN] , cur[MAXN] , vis[MAXN] ; 
int Q[MAXN] , head , tail ;
int s , t ;
int flow , cost ;
int n , m ;
vector < int > G[2] ;
int val[MAXN][MAXN] ;
int is[MAXN] ;
int Lv[MAXN] ;

void init () {
    cntE = 0 ;
    clr ( H , -1 ) ;
}

void addedge ( int u , int v , int c , int w ) {
    E[cntE] = Edge ( v , c , +w , H[u] ) ;
    H[u] = cntE ++ ;
    E[cntE] = Edge ( u , 0 , -w , H[v] ) ;
    H[v] = cntE ++ ;
}

int spfa () {
    head = tail = 0 ;
    clr ( d , INF ) ;
    Q[tail ++] = s ;
    d[s] = 0 ;
    cur[s] = -1 ;
    while ( head != tail ) {
        int u = Q[head ++] ;
        if ( head == MAXN ) head = 0 ;
        vis[u] = 0 ;
        for ( int i = H[u] ; ~i ; i = E[i].n ) {
            int v = E[i].v ;
            if ( E[i].c && d[v] > d[u] + E[i].w ) {
                d[v] = d[u] + E[i].w ;
                cur[v] = i ;
                if ( !vis[v] ) {
                    vis[v] = 1 ;
                    Q[tail ++] = v ;
                    if ( tail == MAXN ) tail = 0 ;
                }
            }
        }
    }
    if ( d[t] >= 0 ) return 0 ;
    cost += d[t] ;
    flow ++ ;
    for ( int i = cur[t] ; ~i ; i = cur[E[i ^ 1].v] ) {
        E[i].c -- ;
        E[i ^ 1].c ++ ;
    }
    return 1 ;
}

int mcmf () {
    flow = cost = 0 ;
    while ( spfa () ) ;
    return cost ;
}

void solve () {
    int lv , atk , x , y , v ;
    int sum = 0 ;
    init () ;
    scanf ( "%d%d" , &n , &m ) ;
    s = 0 ;
    t = n + 1 ;
    G[0].clear () ;
    G[1].clear () ;
    clr ( val , 0 ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        scanf ( "%d%d%d" , &is[i] , &Lv[i] , &atk ) ;
        G[is[i]].push_back ( i ) ;
        if ( !is[i] ) {
            addedge ( s , i , 1 , atk ) ;
            //printf ( "s->%d\n" , i ) ;
        } else {
            addedge ( i , t , 1 , atk ) ;
            //printf ( "%d->t\n" , i ) ;
        }
        sum += atk ;
    }
    for ( int i = 1 ; i <= m ; ++ i ) {
        scanf ( "%d%d%d" , &lv , &atk , &v ) ;
        if ( v == 2 ) {
            scanf ( "%d%d" , &x , &y ) ;
            if ( is[x] ) swap ( x , y ) ;
            val[x][y] = max ( val[x][y] , atk ) ;
        } else if ( v == 1 ) {
            scanf ( "%d" , &x ) ;
            if ( is[x] == 0 ) {
                for ( int j = 0 ; j < G[1].size () ; ++ j ) {
                    int y = G[1][j] ;
                    if ( Lv[x] + Lv[y] == lv ) val[x][y] = max ( val[x][y] , atk ) ;
                }
            } else {
                for ( int j = 0 ; j < G[0].size () ; ++ j ) {
                    int y = G[0][j] ;
                    if ( Lv[x] + Lv[y] == lv ) val[y][x] = max ( val[y][x] , atk ) ;
                }
            }
        } else {
            for ( int j = 0 ; j < G[0].size () ; ++ j ) {
                int x = G[0][j] ;
                for ( int k = 0 ; k < G[1].size () ; ++ k ) {
                    int y = G[1][k] ;
                    if ( Lv[x] + Lv[y] == lv ) val[x][y] = max ( val[x][y] , atk ) ;
                }
            }
        }
    }
    for ( int i = 0 ; i < G[0].size () ; ++ i ) {
        int x = G[0][i] ;
        for ( int j = 0 ; j < G[1].size () ; ++ j ) {
            int y = G[1][j] ;
            if ( val[x][y] ) {
                addedge ( x , y , INF , -val[x][y] ) ;
                //printf ( "%d->%d\n" , x , y ) ;
            }
        }
    }
    printf ( "%d\n" , sum - mcmf () ) ;
}

int main () {
    int T ;
    scanf ( "%d" , &T ) ;
    for ( int i = 1 ; i <= T ; ++ i ) {
        solve () ;
    }
    return 0 ;
}

你可能感兴趣的:(【HDU】5383 Yu-Gi-Oh!【费用流】)