poj 2724 Purifying Machine

Posted ZefengYao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2724 Purifying Machine相关的知识,希望对你有一定的参考价值。

Purifying Machine
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5408   Accepted: 1575

Description

Mike is the owner of a cheese factory. He has 2N cheeses and each cheese is given a binary number from 00...0 to 11...1. To keep his cheese free from viruses, he made himself a purifying machine to clean virus-infected cheese. As a talented programmer, his purifying machine is built in a special way. His purifying machine has N switches, each switch has three states, 1, 0 and *. An operation of this machine is a cleaning action according to the states of the N switches. During one operation, at most one switch can be turned to state *, which can substitute for either 1 or 0. When the machine is turned to a specific state, an operation will clean all the cheeses with corresponding binary numbers. For example, if N equals 6 and the switches are turned to 01*100, the cheeses numbered 010100 and 011100 are under operation by the machine. 

One day, Mike‘s machine was infected. When Mike found out, he had already done some operations and the cheeses operated by this infected machine were infected too. He cleaned his machine as quickly as he could, and now he needs to clean the infected cheeses with the minimum number of operations. If a cheese is infected, cleaning this cheese with the machine one or more times will make this cheese free from virus again; but if a cheese is not infected, operation on this cheese will make it go bad. 

Now given the infected operations Mike has done, you need to find out the minimum number of operations that must be performed to clean all the infected cheeses without making any clean cheese go bad.

Input

There are several test cases. Each test case starts with a line containing two numbers N and M (1 <= N <= 10, 1 <= M <= 1000). N is the number of switches in the machine and M is the number of infected operations Mike has done. Each of the following M lines contains a switch state of the machine. A test case with N = M = 0 ends the input and should not be processed.

Output

For each test case, output one line containing an integer, which is the minimum number of operations Mike needs to do.

Sample Input

3 3
*01
100
011
0 0

Sample Output

2

翻译:mike搞了一台机器专门清洗中毒的奶酪,有一天,机器也中毒了并且开始自顾自的清洗奶酪,被中毒的机器清洗的奶酪也会中毒,mike即时发现了这个问题,修复机器后,想要以最少的操作数,让之前中毒的奶酪回复正常。
思路:如果一个操作带有‘*’,那么机器将会同时清理两个奶酪,那么在最后清理奶酪时,尽量使用带‘*‘号的操作可以更快的清理完奶酪,若我们建图,并且让中毒奶酪的编号相差1的奶酪连一条边,这些连边的两块奶酪使用‘*’操作来清理1次就行,这相当于找一个最大独立集,集合中每两块奶酪都没有连边,若把集合中的奶酪全部清理了,就相当于把所有中毒奶酪都清洗了;
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<bitset>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = 1100;
int V;//点的个数
vector<int>G[N_MAX];
int match[N_MAX];
bool used[N_MAX];
void add_edge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
}

bool dfs(int v) {
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++) {
        int u = G[v][i], w = match[u];
        if (w < 0 || !used[w] && dfs(w)) {
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}

int bipartite_matching() {
    int res = 0;
    memset(match, -1, sizeof(match));
    for (int v = 0; v < V; v++) {
        if (match[v] < 0) {
            memset(used, 0, sizeof(used));
            if (dfs(v))
                res++;
        }
    }
    return res;
}

int N, M;
string s[N_MAX];
vector<int>operation;
int main() {
    while (scanf("%d%d",&N,&M)&&N) {
        //operation.resize(N);
        for (int i = 0; i < M;i++) 
            cin >> s[i];
        for (int i = 0; i < M; i++) {
            int x=0;
            for (int j = 0; j < s[i].size();j++) {
                if(s[i][j]==1)x += 1 << (N - j - 1);
            }
            operation.push_back(x);
            for (int j = 0; j < s[i].size();j++) {
                if (s[i][j] == *)x += 1 << (N - j - 1);
            }
            operation.push_back(x);
        }
        sort(operation.begin(),operation.end());
        operation.erase(unique(operation.begin(),operation.end()),operation.end());
        V = operation.size();
        for (int i = 0; i < V;i++) {
            for (int j = i + 1; j < V;j++) {
                bitset<32>bit = operation[i] ^ operation[j];//找找二进制有几位是一样的
                if (bit.count() == 1) {//只有一位是一样的,可以让机器使用‘*‘操作,符合连边条件
                    add_edge(i, j);
                }
            }
        }
        printf("%d\n",V-bipartite_matching());
        for (int i = 0; i < V;i++) {
            G[i].clear();
        }
        operation.clear();
    }
    return 0;
}

 

以上是关于poj 2724 Purifying Machine的主要内容,如果未能解决你的问题,请参考以下文章

poj2724

[精]poj2724

POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

UVa1663 Purifying Machine (二分图最大匹配,最大流)

DP Intro - Tree POJ2342 Anniversary party

BZOJ 2724 2724: [Violet 6]蒲公英 (区间众数不带修改版本)