hdu 1281 棋盤遊戲 二分圖匹配 匈牙利算法 暴力

D - 棋盤遊戲
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

小希和Gardon在玩一個遊戲:對一個N*M的棋盤,在格子裏放盡量多的一些國際象棋裏面的“車”,並且使得他們不能互相攻擊,這當然很簡單,但是Gardon限制了只有某些格子纔可以放,小希還是很輕鬆的解決了這個問題(見下圖)注意不能放車的地方不影響車的互相攻擊。 
所以現在Gardon想讓小希來解決一個更難的問題,在保證儘量多的“車”的前提下,棋盤裏有些格子是可以避開的,也就是說,不在這些格子上放車,也可以保證儘量多的“車”被放下。但是某些格子若不放子,就無法保證放盡量多的“車”,這樣的格子被稱做重要點。Gardon想讓小希算出有多少個這樣的重要點,你能解決這個問題麼? 

Input

輸入包含多組數據, 
第一行有三個數N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盤的高、寬,以及可以放“車”的格子數目。接下來的K行描述了所有格子的信息:每行兩個數X和Y,表示了這個格子在棋盤中的位置。 

Output

對輸入的每組數據,按照如下格式輸出: 
Board T have C important blanks for L chessmen. 

Sample Input

3 3 4
1 2
1 3
2 1
2 2
3 3 4
1 2
1 3
2 1
3 2

Sample Output

Board 1 have 0 important blanks for 2 chessmen.
Board 2 have 3 important blanks for 3 chessmen.


解題思路:

先求出最大匹配,然後枚舉去掉某一個點,如果去掉後,最大匹配少了,那這個點就是重要點。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#include <map>
#include <set>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e4 + 100;
int n, m, k;
bool gra[110][110];
int match[110];
bool vis[110];

//能否找到增廣路徑
bool Augment(int u) {
    vis[u] = true;
    for (int i = 1; i <= m; ++i) {
        if (!gra[u][i]) {
            continue;
        }
        if (!match[i] || !vis[match[i]] && Augment(match[i])) {
            match[i] = u;
            return true;
        }
    }
    return false;
}

//匈牙利算法:
//用增廣路徑求二分圖最大匹配
int Hungary() {
    int ans = 0;
    memset(match, 0, sizeof(match));
    for (int i = 1; i <= n; ++i) {
        memset(vis, 0, sizeof(vis));
        if (Augment(i)) {
            ++ans;
        }
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    int cas = 0;
    while (scanf("%d%d%d", &n, &m, &k) != EOF) {
        memset(gra, 0, sizeof(gra));
        for (int i = 1, x, y; i <= k; ++i) {
            scanf("%d%d", &x, &y);
            gra[x][y] = true;
        }
        int mat = Hungary();
        int important = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (gra[i][j]) {
                    gra[i][j] = false;
                    if (mat > Hungary()) {
                        ++important;
                    }
                    gra[i][j] = true;
                }
            }
        }
        printf("Board %d have %d important blanks for %d chessmen.\n", ++cas, important, mat);
    }
    return 0;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章