• POJ 2724


    Purifying Machine
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 4014   Accepted: 1127

    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

    Source

     
    找出所有被感染的元素,然后对两个元素满足二进制位有且仅有一个不相同的建一条边,不难证明此图是二分图,每多一条匹配边可以减少一次操作,求出最大匹配减掉即可
     
     1 #include <iostream>
     2 #include <vector>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 #define maxn 2005
    10 
    11 int n,m,len,ans;
    12 int ele[maxn],match[maxn];
    13 vector<int> G[maxn];
    14 bool vis[maxn];
    15 
    16 bool judge(int x1,int x2) {
    17         int sum = 0;
    18         for(int i = 0; i < 10; ++i) {
    19                 if((x1 >> i & 1) ^ (x2 >> i & 1)) ++sum;
    20         }
    21 
    22         return sum == 1;
    23 }
    24 void build() {
    25     for(int i = 0; i < len; ++i) {
    26             for(int j = i + 1; j < len; ++j) {
    27                     if(judge(ele[i],ele[j])) {
    28                             G[i].push_back(j);
    29                             G[j].push_back(i);
    30                     }
    31             }
    32     }
    33 
    34 }
    35 
    36 bool dfs(int u) {
    37         for(int i = 0; i < G[u].size(); ++i ) {
    38                 int v = G[u][i];
    39                 if(vis[v]) continue;
    40                 vis[v] = 1;
    41                 if(match[v] == -1 || dfs(match[v])) {
    42                         match[v] = u;
    43                         return true;
    44                 }
    45         }
    46 
    47         return false;
    48 }
    49 
    50 void solve() {
    51 
    52         for(int i = 0; i < len; ++i) G[i].clear();
    53         build();
    54 
    55         for(int i = 0; i < len; ++i) {
    56                 match[i] = -1;
    57         }
    58 
    59         for(int i = 0; i < len; ++i) {
    60                 memset(vis,0,sizeof(vis));
    61                 if(dfs(i)) ++ans;
    62         }
    63 }
    64 
    65 int main()
    66 {
    67    // freopen("sw.in","r",stdin);
    68     while(~scanf("%d%d",&n,&m)) {
    69             len = 0;
    70             ans = 0;
    71             if(!n && !m) break;
    72             char ch[20];
    73             for(int i = 0; i < m; ++i) {
    74                     scanf("%s",ch);
    75                     int pos = -1,sum = 0;
    76                     for(int j = 0; j < n; ++j) {
    77                             if(ch[j] == '1')
    78                                     sum += (1 << (n - j - 1));
    79                             if(ch[j] == '*')
    80                                     pos = n - j - 1;
    81                     }
    82 
    83                     ele[len++] = sum;
    84                     if(pos != -1) ele[len++] = sum + (1 << pos);
    85 
    86             }
    87 
    88             sort(ele,ele + len);
    89             len = unique(ele,ele + len) - ele;
    90 
    91             solve();
    92 
    93             printf("%d
    ",len - (ans / 2));
    94     }
    95 
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    Android 工程师眼里的大前端:GMTC 2018 参会总结
    Android 工程师眼里的大前端:GMTC 2018 参会总结
    Android 工程师眼里的大前端:GMTC 2018 参会总结
    你所不知道的Python | 字符串连接的秘密
    你所不知道的Python | 字符串连接的秘密
    你所不知道的Python | 字符串连接的秘密
    你所不知道的Python | 字符串连接的秘密
    java基础(一)
    java基础(一)
    《SQL Server企业级平台管理实践》读书笔记——关于SQL Server数据库的还原方式
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3602279.html
Copyright © 2020-2023  润新知