|
| 1 | +//dp, d&c |
| 2 | +//https://codingcompetitions.withgoogle.com/codejam/round/0000000000877ba5/0000000000aa9280 |
| 3 | +#include <stdio.h> |
| 4 | +#include <memory.h> |
| 5 | +#include <algorithm> |
| 6 | +#include <vector> |
| 7 | +#include <queue> |
| 8 | +#define ll long long |
| 9 | +#define pii pair<int,int> |
| 10 | +using namespace std; |
| 11 | +int X[101][101]; |
| 12 | +int com[101][101][101]; |
| 13 | +int cap[101][101],w[101][101]; |
| 14 | +int work(int l,int r) { |
| 15 | + if (w[l][r] == -1) { |
| 16 | + if (l == r) w[l][r] = 0; |
| 17 | + else { |
| 18 | + for (int m = l; m < r; ++m) { |
| 19 | + int cost = work(l, m) + work(m + 1, r); |
| 20 | + int stk = cap[l][m] + cap[m + 1][r] - 2 * cap[l][r]; |
| 21 | + cost += stk + stk; |
| 22 | + if (w[l][r] == -1 || w[l][r] > cost) w[l][r] = cost; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + return w[l][r]; |
| 27 | +} |
| 28 | +int main() { |
| 29 | + int T, tc; |
| 30 | + for (scanf("%d", &T), tc = 1; tc <= T; ++tc) { |
| 31 | + int i,j, E, W; scanf("%d%d", &E, &W); |
| 32 | + memset(cap, 0, sizeof(cap)); |
| 33 | + memset(w, -1, sizeof(w)); |
| 34 | + |
| 35 | + for (i = 1; i <= E; ++i) { |
| 36 | + for (j = 1; j <= W; ++j) scanf("%d", X[i] + j),com[j][i][i]=X[i][j]; |
| 37 | + |
| 38 | + } |
| 39 | + for (i = 1; i <= W; ++i) { |
| 40 | + for (int l = 1; l <= E; ++l) { |
| 41 | + for (int r = 1+l; r <= E; ++r) { |
| 42 | + com[i][l][r] = min(com[i][l][r - 1], com[i][r][r]); |
| 43 | + cap[l][r] += com[i][l][r]; |
| 44 | + } |
| 45 | + cap[l][l] += com[i][l][l]; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + printf("Case #%d: %d\n", tc, work(1,E)+cap[1][E]*2); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + return 0; |
| 55 | +} |
0 commit comments