[HDU 5383] Yu-Gi-Oh!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5383

很显然的二分图最大权匹配, 边上的费用就是合成怪兽所增加的攻击力。 为了防止TLE, 对于那些重复的边留下最大的那条即可。 最坑爹的是, 当做费用流来写时, 由于不需要满流, 要在每次增广后的答案之间再取个max。

#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1000;
const int M = 1000000;
const int inf = 1 << 30;

int n, m, s, t; 
int cnt, lst[N], nxt[M], to[M], f[M], w[M];
int ans, sum, tun[N], lev[N], atk[N], G[N][N];

void add(int u, int v, int flow, int cst){
    nxt[++ cnt] = lst[u]; lst[u] = cnt; to[cnt] = v; f[cnt] = flow; w[cnt] = cst;
    nxt[++ cnt] = lst[v]; lst[v] = cnt; to[cnt] = u; f[cnt] = 0; w[cnt] = -cst;
}

int head, tail, que[M], in[N], dis[N], pre[N];
bool spfa(){
    head = tail = 0;
    for (int i = 1; i <= t; i ++) in[i] = pre[i] = 0, dis[i] = inf;
    dis[s] = 0; que[tail = 1] = s;
    while (head < tail){
        int u = que[++ head]; in[u] = 0;
        for (int j = lst[u]; j; j = nxt[j]){
            int v = to[j];
            if (!f[j]) continue;
            if (dis[u] + w[j] < dis[v]){
                dis[v] = dis[u] + w[j];
                pre[v] = j;
                if (!in[v]){
                    in[v] = 1;
                    que[++ tail] = v;
                }
            }
        }
    }

    return dis[t] != inf;
}
void update(){
    sum -= dis[t];
    ans = max(ans, sum);
    for (int k = pre[t]; k; k = pre[to[k ^ 1]])
        f[k] --, f[k ^ 1] ++;
}

int main(){
    int T;
    for (scanf("%d", &T); T --; ){
        scanf("%d %d", &n, &m);

        cnt = 1; s = n + 1, t = s + 1;
        memset(lst, 0, sizeof(lst));
        memset(G, 0, sizeof(G));

        ans = sum = 0;

        for (int i = 1; i <= n; i ++)
            scanf("%d %d %d", tun + i, lev + i, atk + i), sum += atk[i];

        while (m --){
            int Lev, Atk, r, x, x1, x2;
            scanf("%d %d %d", &Lev, &Atk, &r);
            if (r == 2){
                scanf("%d %d", &x1, &x2);
                if (tun[x1]) swap(x1, x2);
                G[x1][x2] = max(G[x1][x2], Atk - atk[x1] - atk[x2]);
            }
            else if (r == 1){
                scanf("%d", &x);
                for (int i = 1; i <= n; i ++)
                    if (tun[i] != tun[x] && lev[i] + lev[x] == Lev){
                        x2 = i; x1 = x;
                        if (tun[x1]) swap(x1, x2);
                        G[x1][x2] = max(G[x1][x2], Atk - atk[x1] - atk[x2]);
                    }
            }
            else{
                for (x1 = 1; x1 <= n; x1 ++)
                    for (x2 = 1; x2 <= n && !tun[x1]; x2 ++)
                        if (tun[x1] != tun[x2] && lev[x1] + lev[x2] == Lev)
                            G[x1][x2] = max(G[x1][x2], Atk - atk[x1] - atk[x2]);
            }
        }

        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                if (G[i][j]) add(i, j, 1, -G[i][j]);

        for (int i = 1; i <= n; i ++)
            if (!tun[i]) add(s, i, 1, 0);
            else add(i, t, 1, 0);

        ans = sum;

        while (spfa()) update();

        printf("%d\n", ans);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值