zoukankan      html  css  js  c++  java
  • (二进制枚举+bitset)

    问题 J: Automatic Control Machine

    时间限制: 1 Sec  内存限制: 128 MB
    提交 状态

    题目描述

    The company has produced an Automatic Control Machine (ACM for short) that is very popular. Due to its complete and powerful features, the company is preparing to redesign after years of sales. The new version of the ACM is still subject to a number of tests to determine the reliability of the product before it goes on the market. Because there are so many features, each test dataset can only detect several of them. Of course, the product must be available after all features have been tested. Since each test has time and material costs, they like to do the test as less as possible. Assume that running each test dataset costs the same, your
    job is finding the minimum number of test datasets that can cover the test of all features. For example, if there are 5 features that need to be tested, and there are 6 test datasets each can cover the features as follows:
    • Test dataset a: 1
    • Test dataset b: 2, 5
    • Test dataset c: 2, 3, 4
    • Test dataset d: 1, 3, 5
    • Test dataset e: 1, 3, 4
    • Test dataset f: 3, 5
    Although { a, b, c } may do the job, but { c, d } will do the job better in the way of saving time and money.

    输入

    The first line of the input file contains one positive integer T representing the number of machines. For each machine, the first line consists of two integers n and m representing the features of machine that has to be tested and the number of test datasets. It follows by m lines, each line has a binary string of length n, showing whether the features can be detected by the test dataset or not (1 means yes, 0 means no).

    输出

    Output T lines. Each of them should be the minimum number of test dataset needed to test all features for that machine. If it is not possible to test all functions for the machine, output -1.

    样例输入 Copy

    5
    3 3
    100
    011
    111
    5 6
    10000
    01001
    01110
    00111
    10110
    00101
    6 7
    000010
    011000
    100100
    001000
    000010
    010000
    110001
    7 6
    1001001
    1001000
    0001101
    0010110
    0110011
    0100001
    2 1
    01
    

    样例输出 Copy

    1
    2
    4
    3
    -1
    

    提示

    • The number of machines 0 < T ≤ 10
    • The number of functions to be tested 0 < n ≤ 500
    • The number of test data 0 < m ≤ 15
    就是有n道题,m个机器,不同的机器对每一个题如果是0就不能解决,1就是能解决,问就是最少用几个机器可以把所有的题都解决
     
    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
    int n,m;
    int a[510][510];
    int num[510];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            n=read(),m=read();
            int sum=0;
            for(int i=0;i<m;i++)
            {
                string str;
                cin>>str;
                int s=0;
                for(int j=0;j<n;j++)
                {
                    a[i][j]=str[j]-'0';
                    num[j]+=a[i][j];
                }
            }
            bool flag=false;
            for(int i=0;i<n;i++){
                if(num[i]==0){
                    flag=true;
                } 
                num[i]=0;
            }
            if(flag){
                puts("-1");
            } 
            if(!flag)
            {
                int ans=1e9+10;
                for(int i = 1;i < (1 << m);i++)
                { 
                    int cnt = 0;
                    for(int j=0;j<m;j++)
                    {
    //                    if(i&(1<<j))
                        if((i>>j)&1) 
                        {
                            cnt++;
                            for(int k=0;k<n;k++){
                                num[k]+=a[j][k];
                            } 
                        }
                    }
                    int flag=1;
                    for(int j=0;j<n;j++){
                        if(num[j]==0){
                            flag=0;
                        }
                        num[j]=0;
                    }
                    if(flag==1){
                        ans=min(ans,cnt);
                    }  
                }
                cout<<ans<<endl;
            }
            
        }
        return 0;
    }
    /*
    5 6
    10000
    01001
    01110
    00111
    10110
    00101
    
    22433
    
    12433
    11432
    11432
    11321
    11321
    11321
    
    
    */
     bitset的用法
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <stack>
    #include <map>
    #include <sstream>
    #include <cstring>
    #include <set>
    #include <cctype>
    #include <bitset>
    #define IO                       
        ios::sync_with_stdio(false); 
        // cin.tie(0);                  
        // cout.tie(0);
    using namespace std;
    typedef long long LL;
    const int maxn = 2e4 + 10;
    const int maxm = 1e6 + 10;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int inf = 0x3f3f3f3f;
    const LL mod = 11092019;
    int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
    bitset<510> a[20];
    int main()
    {
        int T, r, c;
        cin >> T;
        while (T--)
        {
            cin >> c >> r;
            char ch;
            for (int i = 0; i < r; i++)
                for (int j = 0; j < c; j++)
                {
                    cin >> ch;
                    if (ch == '1')
                        a[i][j] = 1;
                    else
                        a[i][j] = 0;
                }
            int ans = inf;
            for (int k = 0; k < (1 << r); k++) // 枚举所有子集
            {
                bitset<510> t;
                int cnt = 0;
                for (int i = 0; i < r; i++)
                {
                    if (k & (1 << i))
                        t |= a[i], cnt++;
                }
                bool flag = 1;
                for (int i = 0; i < c; i++)
                    if (t[i] == 0)
                    {
                        flag = 0;
                        break;
                    }
                if (flag == 1)
                    ans = min(ans, cnt);
            }
            if (ans == inf)
                cout << -1 << endl;
            else
                cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    python学习第四天 --字符编码 与格式化及其字符串切片
    Lambda表达式 之 C#
    python学习第三天 --布尔类型
    深入理解正则表达式
    《你不知道的JavaScript》第一部分:作用域和闭包
    jquery的extend和fn.extend
    HttpModule与HttpHandler详解
    jQuery分析(2)
    jQuery分析(1)
    jQuery源码中的“new jQuery.fn.init()”什么意思?
  • 原文地址:https://www.cnblogs.com/lipu123/p/13658636.html
Copyright © 2011-2022 走看看