hdu1498最小点集覆盖

对于每一种颜色的气球都建一次图求最大匹配即可。

/*

 * hdu1498/win.cpp

 * Created on: 2012-8-16

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

const int MAXN = 200;

vector<int> mymap[MAXN];

int N, M, mymatch[MAXN];

bool visited[MAXN];

int matrixs[MAXN][MAXN];

void init() {

    for(int i = 0; i < N; i++) {

        mymap[i].clear();

    }

}

bool dfs(int k) {

    int t, I;

    for(int i = 0; i < (int)mymap[k].size(); i++) {

        I = mymap[k][i];

        if(!visited[I]) {

            visited[I] = true;

            t = mymatch[I];

            mymatch[I] = k;

            if(t == -1 || dfs(t)) {

                return true;

            }

            mymatch[I] = t;

        }

    }

    return false;

}

int hungary () {

    memset(mymatch, -1, sizeof(mymatch));

    int ans = 0;

    for (int i = 0; i < N; i++) {

        memset(visited, false, sizeof(visited));

        if (dfs(i)) {

            ans++;

        }

    }

    return ans;

}

bool buildgraph(int k) {

    init();

    for (int i = 0; i < N; i++) {

        for(int j = 0; j < N; j++) {

            if(matrixs[i][j] == k) {

                mymap[i].push_back(j);

            }

        }

    }

    return true;

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    int k;

    bool flags[52];

    char str[500];

    while(scanf("%d%d", &N, &k) == 2) {

        if(N == 0 && k == 0) {

            break;

        }

        for(int i = 0; i < N; i++) {

            for(int j = 0; j < N; j++) {

                scanf("%d", &matrixs[i][j]);

            }

        }

        for(int i = 1; i <= 50; i++) {

            buildgraph(i);

            flags[i] = (hungary() <= k);

        }

        char *p = str;

        for(int i = 1; i <= 50; i++) {

            if(!flags[i]) {

                p += sprintf(p, "%d ", i);

            }

        }

        if(p == str) {

            puts("-1");

        }else {

            int len = strlen(str);

            str[len - 1] = 0;

            puts(str);

        }

    }

    return 0;

}

你可能感兴趣的:(HDU)