poj 2724 Purifying Machine
阿新 • • 發佈:2017-07-23
ras set class desc mac make miss sel 我們 Purifying 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.
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.
For each test case, output one line containing an integer, which is the minimum number of operations Mike needs to do.
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
Output
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