Purifying Machine
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5027   Accepted: 1455

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

题意:

题意:迈克有一台可以净化奶酪的机器,用二进制表示净化的奶酪的编号。但是,在某些二进制串中可能包含有‘*'。例如01*100,'*'其实就代表可以取0,1两种情况--> 010100 和011100。现在由于迈克不小心,他以同样的方式弄脏了某些奶酪,问你最少用多少次操作就可以把弄脏的奶酪全净化好。(没有被弄脏过的奶酪不能净化。弄脏过的奶酪可以多次净化。)

思路:

也就是给你一些不同的(判重之后)二进制串,每个串可以通过1次操作净化,也可以把两个只有1位不同的串通过1次操作联合净化.要我们求最少的操作次数.

我们把所有串按其中1的个数和是奇还是偶分成左右两个点集.

对于任意两个串,如果他们只有1位不同,那么就在他们之间连接一条无向边.(这两个串一定分别属于不同的点集)

由于串的总数是固定的,且一个串可以通过单独净化也可以通过联合净化.而我们向让净化的次数最少,我们自然想联合净化(即一次可以净化两个串)的次数尽量多了. 那么我们最多可以进行多少次联合净化呢? 这个数值==我们建立二分图的最大匹配边数.(想想是不是,因为一个串最多只能被净化一次)

假设总的不同串有n个,我们建立二分图的最大匹配数(即联合净化最大次数)为ans,那么我们总共需要n-ans次净化即可.(想想为什么)

当然本题也可以不用把串特意分成左右点集(本程序实现就是用的这种方式:未分左右点集),我们只需要把原图翻倍,然后求翻倍图的最大匹配数ans,最后用n-ans/2即可.

这个题中有很多位运算的有意思的东西

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=+;
int uN,vN;
int g[N][N];
int link[N];
int used[N];
int num[N];
int dfs(int u){
for(int v=;v<vN;v++)//顶点编号从0开始的
if(g[u][v]&&!used[v]){
used[v]=;
if(link[v]==-||dfs(link[v])){
link[v]=u;
return ;
}
}
return ;
}
int hungary(){
int res=;
memset(link,-,sizeof(link));
for(int u=;u<uN;u++){
memset(used,,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
int main(){
int n,m;
char str[];
while(scanf("%d%d",&n,&m)==&&(m+n)){
memset(g,,sizeof(g));
memset(num,,sizeof(num));
int cnt=;
while(m--){
cnt++;
scanf("%s",&str);
int pos=-;
for(int i=;i<n;i++){
if(str[i]=='*'){pos=i;continue;}
num[cnt]|=(str[i]-'')<<i;
}
if(pos!=-){
cnt++;
num[cnt]=(num[cnt-]|(<<pos));
}
}
sort(num+,num+cnt+);
num[]=-;
int i,j;
for(j=,i=;i<=cnt;i++){
if(num[j]!=num[i])
num[++j] = num[i];
}
cnt=j;
for(i=;i<=cnt;i++)
for(j=;j<=cnt;j++){
int c=num[i]^num[j];
if(c&&((c&(c-))==))g[num[i]][num[j]]=;
}
uN=vN=;
int ans=cnt-hungary()/;
printf("%d\n",ans);
}
return ;
}

[精]poj2724的更多相关文章

  1. MVVM大比拼之AngularJS源码精析

    MVVM大比拼之AngularJS源码精析 简介 AngularJS的学习资源已经非常非常多了,AngularJS基础请直接看官网文档.这里推荐几个深度学习的资料: AngularJS学习笔记 作者: ...

  2. MVVM大比拼之vue.js源码精析

    VUE 源码分析 简介 Vue 是 MVVM 框架中的新贵,如果我没记错的话作者应该毕业不久,现在在google.vue 如作者自己所说,在api设计上受到了很多来自knockout.angularj ...

  3. MVVM大比拼之knockout.js源码精析

    简介 本文主要对源码和内部机制做较深如的分析,基础部分请参阅官网文档. knockout.js (以下简称 ko )是最早将 MVVM 引入到前端的重要功臣之一.目前版本已更新到 3 .相比同类主要有 ...

  4. 【转】《从入门到精通云服务器》第六讲—OpenStack基础

    前五期的<从入门到精通云服务器>受到了广泛好评,收到留言,有很多读者对云计算相关的技术非常感兴趣.应观众要求,我们这期要安利一条纯技术内容.准备好瓜子.花生,随小编一起进入OpenStac ...

  5. Replace 删除、替换函数精解示例

    '************************************************************************* '**模 块 名:Replace函数精解示例 '* ...

  6. Filter 数组过滤函数精解示例

    '************************************************************************* '**模 块 名:Filter 数组过滤函数精解示 ...

  7. 《javascript面向对象精要》读书笔记

    <javascript面向对象精要> 买这本书的原因主要是因为作者,Nicholas C. Zakas 牛X闪闪的js专家,读过js高程的应该都知道他,而这本书是他的最新力作,感觉也是js ...

  8. MATLAB神经网络原理与实例精解视频教程

    教程内容:<MATLAB神经网络原理与实例精解>随书附带源程序.rar9.随机神经网络.rar8.反馈神经网络.rar7.自组织竞争神经网络.rar6.径向基函数网络.rar5.BP神经网 ...

  9. 通过实战理解C语言精要——函数篇

      前言 本篇博客是对C语言函数部分的重点内容和细枝末节通过实战得到的经验的总结精炼,不涵盖C语言函数的全部内容,所有提炼内容均来自提炼与实战,阅读需要对函数部分有一定基础,可用于对C语言函数的理解提 ...

随机推荐

  1. c#中的构造方法

    c#基础--类的构造方法   当实例化一个类时,系统会自动对这个类的属性进行初始化 数字型初始化成0/0.0 string类型初始化成null char类型初始化成\0 构造器就是构造方法,能够被重载 ...

  2. Python 面向对象二(转载)

    来源:www.cnblogs.com/wupeiqi/p/4766801.html 三.类成员的修饰符 类的所有成员在上一步骤中已经做了详细的介绍,对于每一个类的成员而言都有两种形式: 1.公有成员, ...

  3. 解決從Ubuntu 12.04升級至12.10之後的Unity顯示問題

    FROM: http://blog.sina.com.cn/s/blog_97ef3ff4010190pe.html#bsh-75-306370781 今天中午經過系統自帶的“檢查更新”軟件從Ubun ...

  4. Intellij Idea如何不显示.idea target文件夹

    Intellij Idea如何不显示.idea target文件夹 学习了:https://jingyan.baidu.com/article/ceb9fb108e26958cac2ba047.htm ...

  5. caffe卷积层代码阅读笔记

    卷积的实现思想: 通过im2col将image转为一个matrix,将卷积操作转为矩阵乘法运算 通过调用GEMM完毕运算操作 以下两个图是我在知乎中发现的,"盗"用一下,确实非常好 ...

  6. 网页计算器 && 简易网页时钟 && 倒计时时钟

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. swoole编译安装/数据库连接池/异步mysql客户端

    一.编译安装php5.6 0.安装必要软件 http://www.cnblogs.com/itfenqing/p/6055138.html 1.下载php5.6.30 http://php.net/d ...

  8. python 搭建环境

    直接命令行里面 1.进入相应的目录 ,然后python,然后python setup.py 2.或者直接python C:\Python27\Lib\site-packages\xlrd-0.9.3\ ...

  9. 強大的Selector框架

    代码地址如下:http://www.demodashi.com/demo/12648.html 前言 在开发的过程中,我们经常会遇到给view设置背景,什么圆形背景啊,圆角背景啊,点击变色背景啊之类的 ...

  10. IPython introduction

    转载:http://blog.csdn.net/gavin_john/article/details/53086766 1. IPython介绍 ipython是一个python的交互式shell,比 ...