-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightOj-1037 - Agent 47.cpp
140 lines (111 loc) · 2.47 KB
/
LightOj-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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define maxn 100010
#define ints size_t
#define pbk push_back
#define intl long long
#define mod 1000000007
#define inf 1000000000
#define pi 2 * acos(0.0)
#define sf(n) scanf("%d", &n)
#define sd(n) scanf("%lf", &n)
#define sl(n) scanf("%lld", &n)
#define sc(c) scanf(" %c", &c)
#define ss(str) scanf("%s", str)
#define pc(c) printf("%c\n", c)
#define pf(n) printf("%d\n", n)
#define pd(n) printf("%lf\n", n)
#define pl(n) printf("%lld\n", n)
#define ps(str) printf("%s\n", str)
#define ms(n, m) memset(n, m, sizeof(n))
char str[20];
int n, arr[15][15], brr[15], crr[15], cost[15][10], dp[15][1 << 16];
void load(int idx)
{
for(int i = 0; i < n; ++i)
{
if(arr[idx][i])
{
++cost[i][arr[idx][i]];
brr[i] = max(brr[i], arr[idx][i]);
}
}
}
void reload(int idx)
{
for(int i = 0; i < n; ++i)
{
if(cost[i][arr[idx][i]])
{
--cost[i][arr[idx][i]];
for(int j = 9; j > 0; --j)
{
if(cost[i][j])
{
brr[i] = j;
break;
}
j == 1 ? brr[i] = 1 : brr[i] = brr[i];
}
}
}
}
void clr()
{
for(int j = 0; j < 15; ++j)
{
brr[j] = 1;
for(int k = 0; k < 10; ++k)
cost[j][k] = 0;
}
}
int solve(int idx, int mask, int num)
{
if(num == 1)
return 0;
if(dp[idx][mask] != -1)
return dp[idx][mask];
load(idx);
int ret = inf;
for(int i = 0; i < n; ++i)
{
if(i != idx && !(mask & 1 << i))
{
int cur = (int) ceil((double) crr[i] / brr[i]);
ret = min(ret, cur + solve(i, mask | 1 << idx, num - 1));
}
}
reload(idx);
return dp[idx][mask] = ret;
}
int main()
{
int test, cs = 1;
sf(test);
while(test--)
{
ms(dp, -1);
sf(n);
for(int i = 0; i < n; ++i)
sf(crr[i]);
for(int i = 0; i < n; ++i)
{
ss(str);
for(int j = 0; j < n; ++j)
arr[i][j] = str[j] - '0';
}
int ans = inf;
for(int i = 0; i < n; ++i)
{
clr();
ans = min(ans, crr[i] + solve(i, 0, n));
}
printf("Case %d: %d\n", cs++, ans);
}
/*cin.clear();
cin.ignore();
cin.get();*/
return 0;
}