uva348 optimal matrix chain multiply classic interval DP

Source: Internet
Author: User

Tag:uva348    optimal matrix link     classic interval dp   

uva348 optimal matrix chain multiplication//typical interval dp//dp[i][j] represents the minimum cost of the matrix I to J chain multiplication//dp[i][j] = min (dp[i][k]+dp[k+1][j]+a[i].pl*a[k].pr*a[j].pr) ;//On the interval I to J find a K make dp[i][k]+dp[k+1][j] these two parts and in addition to the last//A[I].PL*A[K].PR*P[I].PR minimum value;//can have such a state key is; P =a[1] * a[2] * .... * A[k]//And q= a[k+1] * a[k+2] * .... * A[n] The results of these two parts are not mutually affecting,//So only the P and Q optimal method to calculate, the optimal sub-structure! Boundary dp[i][i]=0;////This is a classic interval DP topic, learned to use this method to consider the problem//Finally, there is a recursive printing, it is very wonderful. Still very happy. Hey, keep practicing. #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat > #include <climits> #include <cmath> #include <complex> #include <cstdio> #include < cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #define CEIL (A, B) (((a) + (b)-1)/(c)) #define Endl ' \ n ' #define GCD __gcd#defiNE highbit (x) (1ull<< (63-__builtin_clzll (x))) #define Popcount __builtin_popcountlltypedef Long Long ll;using namespace Std;const int MOD = 1000000007;const long Double PI = ACOs ( -1.L); Template<class t> inline T LCM (const T&AM P A, const t& b) {return A/GCD (A, b) *b;} Template<class t> Inline T lowbit (const t& x) {return x&-x;} Template<class t> inline T maximize (t& A, const t& b) {return a=a<b?b:a;} Template<class t> inline T Minimize (t& A, const t& b) {return a=a<b?a:b;} const int MAXN = 1008;struct node {int pl;int PR;} A[maxn];ll d[maxn][maxn];int path[maxn][maxn];int n;const ll inf = 0x4f4f4f4f4f4f4f4f;ll dp (int i,int j) {if (D[i][j]!=inf ) return d[i][j];ll& ans = d[i][j];for (int k=i;k<j;k++) {ll res = DP (i,k) +DP (k+1,j) + (LL) a[i].pl*a[k].pr*a[j].pr; if (ans>res) {ans = res;path[i][j] = k;}} return ans;} void print (int i,int j) {if (i==j) {printf ("a%d", i+1); return;} printf ("("); for (int k=i;k<j;k++) if (path[i][j]==k) {print (i,k);p rintf ("x");p rint (K+1,J); printf (")");//printf (")");} int main () {int kase = 0;//freopen ("G:\\code\\1.txt", "R", stdin), while (scanf ("%d", &n)!=eof&&n) {for (int i= 0;i<n;i++) scanf ("%d%d", &AMP;A[I].PL,&AMP;A[I].PR);//memset (d,0x4f,sizeof (d));//memset (path,0,sizeof (path)); for (int i=0;i<n;i++) for (int j=0;j<n;j++) {path[i][j]=0;d[i][j]=inf;} for (int. i=0;i<n;i++) D[I][I]=0;DP (0,n-1),//printf ("%lld\n", DP (0,n-1));p rintf ("Case%d:", ++kase);p rint (0,n-1); Puts ("");} return 0;}

uva348 optimal matrix chain multiply classic interval DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.