There is such an article, the results of which I want to use: http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
The program from there works correctly: https://ideone.com/5ezfkL
And here is the result of the work of the same program that I am trying to run from VS 2010:
// ΠΠ°ΡΡΠΈΡΠ° Ai ΠΈΠΌΠ΅Π΅Ρ ΡΠ°Π·ΠΌΠ΅ΡΠ½ΠΎΡΡΡ p[i-1] xp[i] Π΄Π»Ρ i = 1..n int MatrixChainOrder(int p[], int n) { /* ΠΠ»Ρ ΠΏΡΠΎΡΡΠΎΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ, Π² m[][] ΠΎΠ΄Π½Π° Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ°Π½Ρ ΡΡΡΠΎΠΊΠ° ΠΈ ΠΎΠ΄ΠΈΠ½ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΡΡΠΎΠ»Π±Π΅Ρ]. ΠΡΠ»Π΅Π²Π°Ρ ΡΡΡΠΎΠΊΠ° ΠΈ Π½ΡΠ»Π΅Π²ΠΎΠΉ ΡΡΠΎΠ»Π±Π΅Ρ Π² m[][] Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡΡΡΡ */ int **m = new int* [n]; for(int i = 0; i < n; i++) m[i] = new int [n]; int i, j, k, L, q; /* m[i,j] = ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΡΠΊΠ°Π»ΡΡΠ½ΡΡ
ΡΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠΉ, Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎΠ΅ Π΄Π»Ρ Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΡ ΠΌΠ°ΡΡΠΈΡΡ A[i]A[i+1]...A[j] = A[i..j], Π³Π΄Π΅ ΡΠ°Π·ΠΌΠ΅ΡΠ½ΠΎΡΡΡ A[i] ΡΠ°Π²Π½Π° p[i-1] xp[i] */ // Π‘ΡΠΎΠΈΠΌΠΎΡΡΡ ΡΠ°Π²Π½Π° Π½ΡΠ»Ρ, ΠΊΠΎΠ³Π΄Π° ΡΠΌΠ½ΠΎΠΆΠ°Π΅ΠΌ ΠΎΠ΄Π½Ρ ΠΌΠ°ΡΡΠΈΡΡ for (i = 1; i < n; i++) m[i][i] = 0; // L - Π΄Π»ΠΈΠ½Π° ΡΠ΅ΠΏΠΎΡΠΊΠΈ for (L=2; L<n; L++) { for (i=1; i<=n-L+1; i++) { j = i+L-1; m[i][j] = INT_MAX; for (k=i; k<=j-1; k++) { // q = ΡΡΠΎΠΈΠΌΠΎΡΡΡ/ΡΠΊΠ°Π»ΡΡΠ½ΡΠ΅ ΡΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΡ q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]; if (q < m[i][j]) m[i][j] = q; } } } return m[1][n-1]; } Example of running the program:
int main(){ const int n = 10; int p[n] = {10, 50, 30, 7, 25, 60, 15, 80, 55, 30}; int cost = 0; cost = MatrixChainOrder(p, n); //std::cout << "Solution for m and s: " << std::endl; std::cout << "min cost: " << cost << std::endl; int pause; std::cin >> pause; } In the debugger, I could not figure out anything: at the last iterations of filling the table, the program suddenly crashes with an access violation, and a file with locks opens. Indexes, as I have noticed, do not go beyond the permissible limits.
And also: in the function we have a memory leak, because the dynamic array is not deleted. But you cannot delete it, because you need to return the value of its cell. What can be done with this?