HDU 3315 My Brute KM算法

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3315

思路:建图很简单,不过因为要使原匹配改变最小,处理方法和HDU 2853一样,不多说了

总结:理解错了题意,当出场次序改变时,它的伤害值对应仍不变,我以为是对应如今的出场次序,然后样例过的很完美。。。醉了醉了。。。

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;

const int N = 100;
const int INF = 0x3f3f3f3f;
int nx, ny;
int lx[N], ly[N], slack[N], match[N], s[N][N];
bool visx[N], visy[N];

bool hungary(int v)
{
    visx[v] = true;
    for(int i = 1; i <= ny; i++)
    {
        if(visy[i]) continue;
        if(lx[v] + ly[i] == s[v][i])
        {
            visy[i] = true;
            if(match[i] == -1 || hungary(match[i]))
            {
                match[i] = v;
                return true;
            }
        }
        else slack[i] = min(slack[i], lx[v] + ly[i] - s[v][i]);
    }

    return false;
}

void km()
{
    memset(match, -1, sizeof match);
    memset(ly, 0, sizeof ly);
    for(int i = 1; i <= nx; i++)
        lx[i] = -INF;
    for(int i = 1; i <= nx; i++)
        for(int j = 1; j <= ny; j++)
            lx[i] = max(lx[i], s[i][j]);
    for(int i = 1; i <= nx; i++)
    {
        memset(slack, 0x3f, sizeof slack);
        while(true)
        {
            memset(visx, 0, sizeof visx);
            memset(visy, 0, sizeof visy);
            if(hungary(i)) break;
            else
            {
                int d = INF;
                for(int j = 1; j <= ny; j++)
                    if(!visy[j]) d = min(d, slack[j]);
                for(int j = 1; j <= nx; j++)
                    if(visx[j]) lx[j] -= d;
                for(int j = 1; j <= ny; j++)
                    if(visy[j]) ly[j] += d;
                    else slack[j] -= d;
            }
        }
    }
}

int main()
{
    int n;
    int v[N], h[N], p[N], a[N], b[N];

    while(scanf("%d", &n), n)
    {
        for(int i = 1; i <= n; i++) scanf("%d", v + i);
        for(int i = 1; i <= n; i++) scanf("%d", h + i);
        for(int i = 1; i <= n; i++) scanf("%d", p + i);
        for(int i = 1; i <= n; i++) scanf("%d", a + i);
        for(int i = 1; i <= n; i++) scanf("%d", b + i);

        memset(s, 0, sizeof s);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
            {
                int t1 = p[j] / a[i] + ((p[j] % a[i] == 0) ? 0 : 1);
                int t2 = h[i] / b[j] + ((h[i] % b[j] == 0) ? 0 : 1);

                if(t1 <= t2) s[i][j] = v[i] * 100;
                else s[i][j] = -v[i] * 100;

                if(i == j) s[i][j] += 1;
            }

        nx = ny = n;
        km();

        int res = 0;
        for(int i = 1; i <= ny; i++)
            res += s[match[i]][i];

        if(res > 0) printf("%d %.3f%%\n", res / 100, 100.0 * (res % 100) / n);
        else printf("Oh, I lose my dear seaco!\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值