348-optimal Array Multiplication Sequence
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=284
Memory Search: dp[a][b] = max (Dp[a][b], Dp[a][i] + dp[i + 1][b] + x[a] * y[i] * y[b])
Complete code:
/*0.089s*/#include <cstdio> #include <cstring> const int MAXN = 15;
int X[MAXN], Y[MAXN], D[MAXN][MAXN], PATH[MAXN][MAXN];
the int dp (int a, int b) {if (D[a][b] >= 0) return d[a][b];
PATH[A][B] = A;
if (a = = B) return d[a][b] = 0;
D[A][B] = -1u >> 1;
int tmp;
for (int i = A; I < b; ++i) {TMP = DP (A, I) + DP (i + 1, b) + x[a] * y[i] * y[b];
if (TMP < D[A][B]) d[a][b] = tmp, path[a][b] = i;
return d[a][b];
} void print (int a, int b) {if (a > B) return;
if (a = = b) printf ("a%d", A + 1);
else {printf ("(");
Print (A, path[a][b]);
printf ("x");
Print (Path[a][b] + 1, b);
printf (")");
int main () {int n, cas = 0;
while (scanf ("%d", &n), N) {memset (d,-1, sizeof (d));
for (int i = 0; i < n; i++) scanf ("%d%d", &x[i], &y[i]);
DP (0, N-1);
printf ("Case%d:", ++cas);
Print (0, n-1);
Putchar (10);
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/