1. 程式人生 > 實用技巧 >(二進位制列舉+bitset)

(二進位制列舉+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 afteryears of sales. The new version of the ACM is still subject to a number of tests to determinethe 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 availableafter all features have been tested. Since each test has time and material costs, they like todo 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. Forexample, if there are 5 features that need to be tested, and there are 6 test datasets each cancover 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 timeand money.

輸入

The first line of the input file contains one positive integer T representing the number ofmachines. For each machine, the first line consists of two integers n and m representing thefeatures of machine that has to be tested and the number of test datasets. It follows by mlines, each line has a binary string of length n, showing whether the features can be detectedby 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


*/