二分图最大权匹配

hdu2255

hdu 6346

分析

同类题还有
Golden Tiger Claw UVA - 11383
这是根据二分图KM算法的性质求解特殊的线性规划问题
6364求得最小,需要取相反数,然后求最大
对于2255 ,普通的O(n^4) 都能通过,
但是对于6364 ,O(n^3) 才能通过
感到诡异的是在6364中通过的代码竟然没有其他的代码在2255中跑的***到窒息

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int N = 210;
int val[N][N];
LL lx[N],ly[N];
int linky[N];
LL pre[N];
bool vis[N];
bool visx[N],visy[N];
LL slack[N];
int n;
void bfs(int k) {
    LL px, py = 0,yy = 0, d;
    memset(pre, 0, sizeof(LL) * (n+2));
    memset(slack, inf, sizeof(LL) * (n+2));
    linky[py]=k;
    do {
        px = linky[py],d = INF, vis[py] = 1;
        for(int i = 1; i <= n; i++)
            if(!vis[i]) {
                if(slack[i] > lx[px] + ly[i] - val[px][i])
                    slack[i] = lx[px] + ly[i] - val[px][i], pre[i] = py;
                if(slack[i] < d)
                    d = slack[i], yy = i;
                }
        for(int i = 0; i <= n; i++)
            if(vis[i])
                lx[linky[i]] -= d, ly[i] += d;
            else
                slack[i] -= d;
        py = yy;
        }
    while(linky[py]);
    while(py)
        linky[py] = linky[pre[py]], py=pre[py];
    }
void KM() {
    memset(lx, 0, sizeof(int)*(n+2));
    memset(ly, 0, sizeof(int)*(n+2));
    memset(linky, 0, sizeof(int)*(n+2));
    for(int i = 1; i <= n; i++)
        memset(vis, 0, sizeof(bool)*(n+2)), bfs(i);
    }
int main() {
    int T;
    scanf("%d", &T);
    for(int _i = 1; _i <= T; _i++) {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                scanf("%d", &val[i][j]);
                val[i][j] = -val[i][j];
                }
            }
        KM();
        LL ans = 0;
        for(int i = 1; i <= n; ++i)
            ans += lx[i] + ly[i];
        printf("Case #%d: %I64d\n", _i, -ans);
        }
    return 0;
    }