HDU 2813 One fihgt one KM水题

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

题目挺有意思的,也很明了,稍微有点坑爹的地方就是名字转换成数字的过程,刚开始用map<string, int>去映射,超时了,然后直接暴力搜,结果过了,于是搜了一下别人的代码,发现基本都是用映射,发现我映射部分写搓了,于是回去改了一下,过了,但是效率并没有直接搜效率高。。。

直接搜:

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

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

bool hungary(int v)
{
    visx[v] = true;
    for(int i = 0; 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 = 0; i < nx; i++)
        lx[i] = -INF;
    for(int i = 0; i < nx; i++)
        for(int j = 0; j < ny; j++)
            lx[i] = max(lx[i], s[i][j]);
    for(int i = 0; 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 tmp = INF;
                for(int j = 0; j < ny; j++)
                    if(!visy[j]) tmp = min(tmp, slack[j]);
                for(int j = 0; j < nx; j++)
                    if(visx[j]) lx[j] -= tmp;
                for(int j = 0; j < ny; j++)
                    if(visy[j]) ly[j] += tmp;
                    else slack[j] -= tmp;
            }
        }
    }
}

int main()
{
    int n, m, k, a;
    char s1[50], s2[50];
    char sss1[220][50], sss2[220][50];

    while(~ scanf("%d%d%d", &n, &m, &k))
    {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                s[i][j] = -INF;

        int cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < k; i++)
        {
            scanf(" %s%s %d", s1, s2, &a);

            int t1, t2;
            bool f = false;
            for(int j = 0; j < cnt1; j++)
                if(strcmp(s1, sss1[j]) == 0)
                {
                    f = true; t1 = j; break;
                }
            if(f == false) t1 = cnt1, strcpy(sss1[cnt1], s1), cnt1++;

            f = false;
            for(int j = 0; j < cnt2; j++)
                if(strcmp(s2, sss2[j]) == 0)
                {
                    f = true; t2 = j; break;
                }
            if(f == false) t2 = cnt2, strcpy(sss2[cnt2], s2), cnt2++;
            s[t1][t2] = -a;
        }

        nx = n, ny = m;
        km();

        int res = 0;
        for(int i = 0; i < ny; i++)
            if(match[i] != -1) res += s[match[i]][i];
        printf("%d\n", -res);
    }

    return 0;
}

映射:

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

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

bool hungary(int v)
{
    visx[v] = true;
    for(int i = 0; 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 = 0; i < nx; i++)
        lx[i] = -INF;
    for(int i = 0; i < nx; i++)
        for(int j = 0; j < ny; j++)
            lx[i] = max(lx[i], s[i][j]);
    for(int i = 0; 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 tmp = INF;
                for(int j = 0; j < ny; j++)
                    if(!visy[j]) tmp = min(tmp, slack[j]);
                for(int j = 0; j < nx; j++)
                    if(visx[j]) lx[j] -= tmp;
                for(int j = 0; j < ny; j++)
                    if(visy[j]) ly[j] += tmp;
                    else slack[j] -= tmp;
            }
        }
    }
}

int main()
{
    int n, m, k, a;
    char s1[50], s2[50];

    while(~ scanf("%d%d%d", &n, &m, &k))
    {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                s[i][j] = -INF;

        map<string, int> map1, map2;
        int cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < k; i++)
        {
            scanf(" %s%s%d", s1, s2, &a);
            if(map1.find(s1) == map1.end()) map1[s1] = cnt1++;
            if(map2.find(s2) == map2.end()) map2[s2] = cnt2++;
            s[map1[s1]][map2[s2]] = -a;
        }

        nx = n, ny = m;
        km();

        int res = 0;
        for(int i = 0; i < ny; i++)
            if(match[i] != -1) res += s[match[i]][i];
        printf("%d\n", -res);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值