• HDU2426 Interesting Housing Problem 最大权值匹配不能完成匹配的处理


    题意:给定一系列的匹配关系,现在有些匹配时不允许的,问最大匹配时多少。

    解法:解决问题的办法就是在计算松弛d值的时候,如果d的值在经过一次可行标的更新后仍然不能得到某个最小值来扩充原图的边集。那么就不存在一个最优权值匹配。注意当值为负的时候就不能够匹配。

    代码如下:

    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    int N, M, E;
    int w[505][505];
    int sx[505], sy[505];
    int lx[505], ly[505];
    int match[505], slack[505];
    
    int path(int u) {
        sx[u] = 1;
        for (int i = 1; i <= M; ++i) {
            if (sy[i] || w[u][i] < 0) continue;
            int t = lx[u] + ly[i] - w[u][i];
            if (!t)    {
                sy[i] = 1;
                if (!match[i] || path(match[i])) {
                    match[i] = u;
                    return true;    
                }
            } else {
                slack[i] = min(slack[i], t);
            }
        }
        return false;
    }
    
    void KM() {
        int can = 1;
        memset(match, 0, sizeof (match));
        memset(lx, 0x80, sizeof (lx));
        memset(ly, 0, sizeof (ly));
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= M; ++j) {
                lx[i] = max(lx[i], w[i][j]);
            }
        }
        for (int i = 1; i <= N && can; ++i) {
            memset(slack, 0x3f, sizeof (slack));
            while (1) {
                memset(sx, 0, sizeof (sx));
                memset(sy, 0, sizeof (sy));
                if (path(i)) break;
                int d = INF;
                for (int j = 1; j <= M; ++j) {
                    if (!sy[j])    d = min(d, slack[j]);
                }
                if (INF == d) {
                    can = 0;
                    break;
                }
                for (int j = 1; j <= N; ++j) {
                    if (sx[j]) lx[j] -= d;
                }
                for (int j = 1; j <= M; ++j) {
                    if (sy[j]) ly[j] += d;
                    else slack[j] -= d;
                }
            }
        }
        if (!can) {
            puts("-1");
            return;
        }
        int ret = 0;
        for (int i = 1; i <= M; ++i) {
            if (match[i]) {
                ret += w[match[i]][i];
            }
        }
        printf("%d\n", ret);
    }
    
    int main() {
        int x, y, ct, ca = 0;
        while (scanf("%d %d %d", &N, &M, &E) != EOF) {
            for (int i = 1; i <= N; ++i) {
                for (int j = 1; j <= M; ++j) {
                    w[i][j] = -INF;
                }
            }
            for (int i = 0; i < E; ++i) {
                scanf("%d %d %d", &x, &y, &ct);
                w[x+1][y+1] = ct;
            }
            printf("Case %d: ", ++ca);
            KM();
        }
        return 0;
    }
  • 相关阅读:
    js操作
    函数知识点补充
    css---position
    css-浮动
    css-边界重叠以及边界塌陷
    css
    css文本类型操作
    POJ 2828 线段树活用
    POJ 3468 线段树
    POJ 3013 SPFA算法,邻接表的使用
  • 原文地址:https://www.cnblogs.com/Lyush/p/3025713.html
Copyright © 2020-2023  润新知