algorithm to improve matrix multiplication

Source: Internet
Author: User
Tags min printf
This problem was done at the time completely without thinking, to DP is also completely do not understand. Now take time to review a bit, look at the DP function part or a face to find the answer, but either speak very complex, or directly to the code, to me caused a particularly big trouble. So I put the test case into, followed by the compiler went again, suddenly enlightened, the original is this is the test part: 0 2 1 dp[1[2]=50
1 3 2 dp[2][3]=1000
0 3 1 dp[1][3]=1200
0 3 2 dp[1][3]=150 See this moment to understand that the application of the two-dimensional dp[i][j] array I for the starting position a0,j represents the end position an know the meaning of the DP array, the motion rules are simple. At the beginning, I didn't know the meaning of DP, which led to the understanding of the problem. Analysis of the test section of the two dp[1][3] should be able to know the role of k in the section code. Note: The data type in the code needs to be changed, this is to re-write this topic, re-understand the motion rules, so the data type is only used int. The data required in the topic is relatively large. So the choice of data type is important. The macro variable inf defined in the code requires 15 9 to pass all test cases (because of this error, the commit code was submitted several times). in the motion, if it is related to the Max function, DP is initialized to a 0, if it is the Min function, especially careful DP initialization value, as far as possible to set a large point (of course, can not let the compiler Error) This is good for Blue bridge this kind of pit problem. learning can not understand the code, resistance, debugging several times, there will be unexpected harvest.
Just know how weak their ability, knowledge blank too much to continue to learn AH. Recommend this blog http://blog.csdn.net/f_zyj?viewmode=contents she explains the topic in particular detail. A look will be.
#include <stdio.h>
#define INF 9999999999
int dp[1005][1005];//i represents the starting point, and J represents the end point 
int num[10005];// Matrix element
int min (int a,int b) {
	return a>b?b:a;
} 
int DP (int n) {
	int i,j,k;
	for (j=2;j<=n;j++) {
		for (i=j-1;i>0;i--) {
			dp[i][i]=dp[j][j]=0;//itself to its own number of operations is zero for 
			(k=i;k<j;k + +) {
				dp[i][j]=min (dp[i][j],dp[i][k]+dp[k+1][j]+num[i-1]*num[k]*num[j])
			;
	}}} Return dp[1][n];//returns the start and end of DP 
	
}
int main (void) {
	int n;//matrix number
	int i,j;
	int temp;
	scanf ("%d", &n);
	for (i=0;i<=n;i++) {
		scanf ("%d", &num[i]);//input of matrix element 
	}
	if (n==1) {
		printf ("%d\n", num[0]* NUM[1]);
		return 0;
	}
	for (i=0;i<=n;i++) {for
		(j=0;j<=n;j++) {
			dp[i][j]=dp[j][i]=inf;//here is an infinitely large value because it is for min 
		}
	}
	TEMP=DP (n);
	printf ("%d\n", temp);
	return 0;
}


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.