Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n'
- template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
- int N;
- int health[15];
- int weapon[15][15];
- int dp[15][1<<15];
- int solve(int i, int mask, vector<int>damage) {
- int &ret = dp[i][mask];
- if(ret!=-1) return ret;
- ret = INT_MAX;
- int cur = (health[i] + damage[i] - 1) / damage[i];
- for(int j=0; j<N; ++j) damage[j] = max(damage[j], weapon[i][j]);
- for(int j=0; j<N; ++j) if(~mask >> j & 1) {
- ret = min(ret, solve(j, mask|(1<<j), damage));
- }
- if(ret==INT_MAX) ret = 0;
- ret += cur;
- return ret;
- }
- void PlayGround(int T) {
- cin >> N;
- for(int i=0; i<N; ++i) for(int j=0; j<(1<<15); ++j)
- dp[i][j] = -1;
- for(int i=0; i<N; ++i) cin >> health[i];
- for(int i=0; i<N; ++i) {
- for(int j=0; j<N; ++j) {
- char c; cin >> c;
- weapon[i][j] = c - '0';
- }
- }
- vector<int>damage(N, 1);
- int ans = INT_MAX;
- for(int i=0; i<N; ++i) ans = min(ans, solve(i, (1<<i), damage));
- cout << "Case " << T << ": " << ans << '\n';
- #ifndef ONLINE_JUDGE
- cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
- #endif
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- #endif
- int T;
- cin >> T;
- for(int i=1; i<=T; ++i)
- PlayGround(i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement