POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP
题目链接:https://vjudge.net/problem/POJ-1651
Multiplication Puzzle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11239 | Accepted: 6980 |
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
Source
Northeastern Europe 2001, Far-Eastern Subregion
题意:
有N张写有数字的卡片排成一行,按一定次序从中拿走N-2张(第1张和最后一张不能拿),每次只拿一张,取走一张卡片的同时,会得到一个分数,分值的计算方法是:要拿的卡片,和它左右两边的卡片,这三张卡片上数字的乘积。按不同的顺序取走N-2张卡片,得到的总分可能不相同,求出给定一组卡片按上述规则拿取的最小得分。
题解:
最优矩阵链乘问题。刘汝佳紫书P277。
1.在取牌的过程中,必定存在一张最后取的牌,可知这张牌左右两边的牌都已经取完了(除了两端点)。于是“最后取的牌”,就把区间分成了两段。由于这两段区间的取牌(即得分情况)互不影响,故可以分开求值。那又怎样才能知道最后取哪张牌才能使得取值最优呢?枚举区间内每一张牌作为最后取的牌,取最优值。
2.对于被分开的左右两段区间,又可各自根据步骤1得到最优值。
3.紫书中区间的写法是左闭右开,但个人觉得不好理解,于是就写成自己所熟悉的闭区间。
记忆化搜索:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <sstream> 11 #include <algorithm> 12 using namespace std; 13 #define pb push_back 14 #define mp make_pair 15 #define ms(a, b) memset((a), (b), sizeof(a)) 16 #define eps 0.0000001 17 typedef long long LL; 18 const int INF = 2e9; 19 const LL LNF = 9e18; 20 const int mod = 1e9+7; 21 const int maxn = 100+10; 22 23 LL p[maxn], dp[maxn][maxn]; 24 25 LL dfs(int l, int r) //区间[l, r]中每个元素都可以取走 26 { 27 if(l>r) return 0; 28 if(dp[l][r]!=LNF) return dp[l][r]; 29 30 for(int k = l; k<=r; k++) 31 dp[l][r] = min(dp[l][r], dfs(l,k-1) + dfs(k+1,r) + p[l-1]*p[k]*p[r+1]); 32 33 return dp[l][r]; 34 } 35 36 int main() 37 { 38 int n; 39 scanf("%d",&n); 40 for(int i = 1; i<=n; i++) 41 scanf("%lld",&p[i]); 42 43 for(int i = 1; i<n; i++) 44 for(int j = 1; j<=n; j++) 45 dp[i][j] = LNF; 46 47 cout<<dfs(2, n-1)<<endl; 48 return 0; 49 }
递推:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <sstream> 11 #include <algorithm> 12 using namespace std; 13 #define pb push_back 14 #define mp make_pair 15 #define ms(a, b) memset((a), (b), sizeof(a)) 16 #define eps 0.0000001 17 typedef long long LL; 18 const int INF = 2e9; 19 const LL LNF = 9e18; 20 const int mod = 1e9+7; 21 const int maxn = 100+10; 22 23 LL p[maxn], dp[maxn][maxn]; 24 25 int main() 26 { 27 int n; 28 scanf("%d",&n); 29 for(int i = 1; i<=n; i++) 30 scanf("%lld",&p[i]); 31 32 memset(dp, 0, sizeof(dp)); 33 for(int l = 2; l<=n-1; l++) 34 for(int r = l; r<=n-1; r++) 35 dp[l][r] = LNF; 36 37 for(int len = 1; len<=n-1; len++) 38 { 39 for(int l = 2; l<=n-1-len+1; l++) //能取的区间为[2, n-1] 40 { 41 int r = l + len - 1; 42 for(int k = l; k<=r; k++) 43 dp[l][r] = min(dp[l][r], dp[l][k-1] + dp[k+1][r] + p[l-1]*p[k]*p[r+1]); 44 } 45 } 46 47 cout<< dp[2][n-1] <<endl; 48 49 }