-
Notifications
You must be signed in to change notification settings - Fork 34
/
1037-Agent-47.cpp
102 lines (71 loc) · 1.26 KB
/
1037-Agent-47.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <limits.h>
#define check(n, pos) (n & (1<<pos))
#define set(n, pos) (n | (1<<pos))
using namespace std;
int power[20];
int n;
int x[20][20];
int dp[1 << 17];
int unset(int a, int i)
{
int mask;
mask = 1 << i;
a = a & ~mask;
return a;
}
int wow(int count, int a)
{
if(count == n) {
return 0;
}
if(dp[a] != -1) {
return dp[a];
}
int maxi;
int c;
maxi = INT_MAX;
for (int i = 0; i < n; i++) {
if(check(a, i) == 0) {
for (int j = 0; j <= n; j++) {
if(check(a, j) != 0 and x[j][i] != 0) {
a = set(a, i);
c = (int) ceil((float) power[i] / (float) x[j][i]);
maxi = min(maxi, c + wow(count + 1, a));
a = unset(a, i);
}
}
}
}
dp[a] = maxi;
return maxi;
}
int main()
{
int t;
int a;
char c;
scanf("%d", &t);
for (int cases = 1; cases <= t; cases++) {
scanf("%d", &n);
memset(dp, -1, sizeof(dp));
a = 0;
a = set(a, n);
for (int i = 0; i < n; i++) {
scanf("%d", &power[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> c;
x[i][j] = (int) (c - '0');
}
}
for (int i = 0; i < n; i++) {
x[n][i] = 1;
}
printf("Case %d: %d\n",cases, wow(0, a));
}
}