矩阵连乘
对于给定的相继n个矩阵{A1,A2, …, An}(其中Ai的维数为pi-1×pi,i=1,2,…,n),
原理:
在i-j之间找到一个轴,分成A,B两部分矩阵相乘
那么dp[i][j]=A的数乘数+B的数乘数+A*B的数乘数
dp[i][j]=0(i==j)
dp[i][j]=min{dp[i][j],dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j]}(i<k<j)
矩阵例子
完成后通过S矩阵得出最优完全加括号方式为:
(( A1 ( A2 A3 ) )A4 )
P.S:
如果需要输出路径怎么做呢?
将对应于dp[i,j]的断开位置k记录在map[i,j]中,在计算出最优值dp[i,j]后,则由map[i,j]可以递归地构造出相应的最优解。
代码见后。
Multiplication Puzzle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8291 | Accepted: 5155 |
Description
The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.
The goal is to take cards in such order as to minimize the total number of scored points.
For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.
The goal is to take cards in such order as to minimize the total number of scored points.
For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
Input
The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.
Output
Output must contain a single integer - the minimal score.
Sample Input
6 10 1 50 50 20 5
Sample Output
3650
#include <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
#define maxn 105
#define min(x,y) x>y?y:x
using namespace std;
int p[maxn];
int dp[maxn][maxn];
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&p[i]);
}
memset(dp,inf,sizeof(dp));
for(int i=1;i<=n;i++){
dp[i][i]=0;
}
for(int r=0;r<n;r++){ //区间长度
for(int i=1;i+r<n;i++){ //起点
for(int k=i;k<i+r;k++){
dp[i][i+r]=min(dp[i][i+r],dp[i][k]+dp[k+1][i+r]+p[i-1]*p[k]*p[i+r]);
}
}
}
printf("%d\n",dp[1][n-1]);
return 0;
}
当然这是比较简单的
如果要记录路径呢?
我们设置一个map记录每次的k是哪一个,然后递归输出
#include <cstdio>
#include <iostream>
#include <cstring>
#define inf 0x3f3f3f3f
#define maxn 105
#define min(x,y) x>y?y:x
using namespace std;
int p[maxn];
int dp[maxn][maxn],map[maxn][maxn];
void Print(int x,int y){
if(x==y){
cout<<"A"<<x;
}
else if(x+1==y){
cout<<"(A"<<x<<"A"<<y<<")";
}
else{
cout<<"(";
Print(x,map[x][y]);
Print(map[x][y]+1,y);
cout<<")";
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&p[i]);
}
memset(dp,inf,sizeof(dp));
for(int i=1;i<=n;i++){
dp[i][i]=0;
}
for(int r=1;r<=n;r++){ //半径
for(int i=1;i+r<=n;i++){ //起点位置
int j=i+r-1;
for(int k=i;k<j;k++){
int t=dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j];
if(dp[i][j]>t){
dp[i][j]=t;
map[i][j]=k;
}
}
}
}
Print(1,n-1);
printf("%d\n",dp[1][n-1]);
return 0;
}