HDU 1281 棋盘游戏(二分图匹配)

题目链接:点击打开链接

思路:经典二分图建图模型, 对于每一个格子, 按照行标建一列, 列标建一列, 然后进行匹配即可, 然后尝试删除每条边, 再进行匹配看看有没有比原匹配小。  复杂度显然会超时。。 数据水了吧。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 100 + 10;
int T,n,m,k,tot,kase = 0;
int use[maxn],from[maxn];
bool mp[maxn][maxn];
bool match(int x) {
    for(int i = 1; i <= m; i++)
    if(!use[i] && mp[x][i]) {
        use[i] = true;
        if(from[i] == -1 || match(from[i])) {
            from[i] = x;
            return true;
        }
    }
    return false;
}
int hungary(int n) {
    tot = 0;
    memset(from, -1, sizeof(from));
    for(int i = 1; i <= n; i++) {
        memset(use, 0, sizeof(use));
        if(match(i)) ++tot;
    }
    return tot;
}
struct node {
    int r, c;
}a[maxn*maxn];
int main() {
    while(~scanf("%d%d%d",&n,&m,&k)) {
        memset(mp, false, sizeof(mp));
        for(int i = 0; i < k; i++) {
            scanf("%d%d",&a[i].r,&a[i].c);
            mp[a[i].r][a[i].c] = true;
        }
        int init = hungary(n), ans = 0;
        for(int i = 0; i < k; i++) {
            mp[a[i].r][a[i].c] = false;
            int cur = hungary(n);
            mp[a[i].r][a[i].c] = true;
            if(cur < init) ++ans;
        }
        printf("Board %d have %d important blanks for %d chessmen.\n",++kase,ans,init);
    }
    return 0;
}


你可能感兴趣的:(HDU,ACM-ICPC,二分图匹配)