UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#207355#3748. 状压 1marcuse100911ms10000kbC++1.5kb2024-07-28 18:14:382024-07-28 20:14:43

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 400 + 10;
int v[N];
int aans,n,m,T;
int val[N];
int a[N][N];
int read(){
	char ch; int sum = 0,flag = 1;
	ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-') flag = -flag; ch = getchar();}
	while(ch >= '0' && ch <= '9'){sum = sum * 10 + ch - '0'; ch = getchar();}
	return sum * flag;
}
int ans[1024 * 1024 * 2 + 1];
void Work1(){
	memset(v,0,sizeof(v));
	for(int i = 1; i <= n; i++){
		int sum = 0;
		for(int j = 1; j <= m; j++){
			sum = sum * 2 + a[i][j];
		}
		v[i] = sum;
	}
	memset(ans,0x3f,sizeof(ans));
	ans[0] = 0;
	for(int i = 0; i <= (1 << m) - 1; i++){
		for(int j = 1; j <= n; j++){
			ans[i | v[j]] = min(ans[i | v[j]],ans[i] + 1);
		}
	}
	printf("%d\n",ans[(1 << m) - 1]);
	return ;
}
void dfs(int id,int tar,int cos){
	if(id == n + 1){
		if(tar == m){
			aans = min(aans,cos);
		}
	    return ;
	}
	int res = 0;
	for(int i = 1; i <= m; i++) {
		if(!val[i] && a[id][i]) res++;
		val[i] += a[id][i];
	}
	dfs(id + 1,tar + res,cos + 1);
	for(int i = 1; i <= m; i++) val[i] -= a[id][i];
	dfs(id + 1,tar,cos);
}
void Work2(){
	aans = m + 1;
	dfs(1,0,0);
	printf("%d\n",aans);
	return ;
}

int main(){
	T = read();
	while(T--){
		n = read(); m = read();
		memset(a,0,sizeof(a));
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				a[i][j] = read();
			}
		}
		if(m <= 20){
			Work1();
		}
		else if(n <= 20){
			Work2(); 
		}
	}
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 2ms
memory: 1804kb

input:

10
10 40
1 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 1 1 0 1 1 1
1 1 1 1 1 1...

output:

3
3
4
3
3
5
4
5
3
2

result:

ok 10 tokens

Test #2:

score: 5
Accepted
time: 2ms
memory: 1808kb

input:

10
10 40
1 0 0 1 0 1 1 1 0 1 1 0 1 1 1 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 1 1
0 1 1 0 0 1...

output:

4
4
3
2
4
4
4
3
4
3

result:

ok 10 tokens

Test #3:

score: 5
Accepted
time: 0ms
memory: 1808kb

input:

10
10 40
1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1
0 0 0 1 0 0...

output:

4
3
3
4
4
4
5
3
3
3

result:

ok 10 tokens

Test #4:

score: 5
Accepted
time: 0ms
memory: 1808kb

input:

10
10 40
0 1 1 0 0 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 0 1 1 1
0 1 0 0 1 1...

output:

4
3
3
4
4
2
3
3
2
3

result:

ok 10 tokens

Test #5:

score: 5
Accepted
time: 0ms
memory: 1804kb

input:

10
10 40
0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 1 1 0 1 1 0 1 1
1 1 1 0 0 0...

output:

4
4
2
3
4
3
3
3
4
3

result:

ok 10 tokens

Test #6:

score: 5
Accepted
time: 0ms
memory: 1808kb

input:

10
10 40
0 0 0 1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 1 0 0 0 1 0 1 0
0 0 1 1 0 0...

output:

3
3
3
4
3
5
3
3
3
4

result:

ok 10 tokens

Test #7:

score: 5
Accepted
time: 3ms
memory: 9996kb

input:

10
40 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 0
0 1 0 0 1 0...

output:

5
4
4
4
3
4
5
3
8
5

result:

ok 10 tokens

Test #8:

score: 5
Accepted
time: 6ms
memory: 10000kb

input:

10
40 10
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0...

output:

4
3
4
4
9
4
4
7
4
7

result:

ok 10 tokens

Test #9:

score: 5
Accepted
time: 5ms
memory: 10000kb

input:

10
40 10
0 0 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0...

output:

4
5
6
4
8
4
4
5
7
7

result:

ok 10 tokens

Test #10:

score: 5
Accepted
time: 4ms
memory: 9996kb

input:

10
40 10
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0...

output:

9
4
3
5
5
4
4
8
8
3

result:

ok 10 tokens

Test #11:

score: 5
Accepted
time: 3ms
memory: 9996kb

input:

10
40 10
0 1 0 0 1 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 1 0 0 1 0 0 0
0 0 0 0 0 0...

output:

6
4
5
8
4
9
4
5
5
6

result:

ok 10 tokens

Test #12:

score: 5
Accepted
time: 10ms
memory: 10000kb

input:

10
40 10
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 1 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 1 0 1 0...

output:

3
4
4
4
4
5
5
8
4
6

result:

ok 10 tokens

Test #13:

score: 5
Accepted
time: 37ms
memory: 9996kb

input:

10
26 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 1 0 0 0
0...

output:

8
8
6
7
8
6
11
7
8
8

result:

ok 10 tokens

Test #14:

score: 5
Accepted
time: 34ms
memory: 10000kb

input:

10
26 15
1 0 0 0 0 0 0 0 0 1 1 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
0...

output:

8
7
10
7
8
8
9
8
8
9

result:

ok 10 tokens

Test #15:

score: 5
Accepted
time: 323ms
memory: 9996kb

input:

10
21 19
0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0...

output:

8
4
5
3
3
8
11
3
12
3

result:

ok 10 tokens

Test #16:

score: 5
Accepted
time: 7ms
memory: 10000kb

input:

10
99 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 ...

output:

4
4
3
4
3
4
3
4
2
4

result:

ok 10 tokens

Test #17:

score: 5
Accepted
time: 34ms
memory: 1808kb

input:

10
15 26
1 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1
0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0...

output:

6
5
5
7
7
5
10
10
7
11

result:

ok 10 tokens

Test #18:

score: 5
Accepted
time: 33ms
memory: 1808kb

input:

10
15 26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1...

output:

13
9
4
10
5
4
7
5
5
5

result:

ok 10 tokens

Test #19:

score: 5
Accepted
time: 408ms
memory: 1808kb

input:

10
19 21
0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0...

output:

10
7
6
4
4
3
3
5
6
6

result:

ok 10 tokens

Test #20:

score: 5
Accepted
time: 0ms
memory: 1808kb

input:

10
4 99
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 ...

output:

2
2
2
2
2
2
2
2
2
2

result:

ok 10 tokens