POJ 1651 Multiplication Puzzle (区间DP/矩阵链乘优化)

来源:互联网 发布:千元机推荐2017知乎 编辑:程序博客网 时间:2024/04/29 14:55
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[105];int dp[105][105];int main(){    int n;  scanf("%d",&n);    //memset(dp,0,sizeof(dp));    for(int i=1;i<=n;i++)    {        scanf("%d",&a[i]);    }    for(int i=1;i<=n;i++)    {        for(int l=1;i+l<=n;l++)        {            int r=i+l;            dp[l][r]=1e9;            for(int k=l;k<=r;k++)            {                dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]+a[l-1]*a[k]*a[r]);            }        }    }    printf("%d\n",dp[2][n]);<span style="white-space:pre">//不能从1开始 </span>因为a【l-1】*a【k】*a【r】} 
<div class="ptt" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue; padding: 10px;"><a target=_blank href="http://vjudge.net/problem/21493/origin" target="_blank" style="color: blue; text-decoration: none;">Multiplication Puzzle</a></div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536KB</td><td width="10px"> </td><td><strong>64bit IO Format:</strong> %lld & %llu</td></tr></tbody></table></div><p class="opt_btn" align="center" style="font-family: Simsun;font-size:14px;"><span style="font-size:14px;color:#333399;"><a target=_blank href="http://vjudge.net/problem/submit/21493" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(211, 211, 211); color: blue; border-radius: 4px; font-size: 12px !important; background: url("../jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png") 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="color: black; display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank href="http://vjudge.net/status/#OJId=POJ&probNum=1651" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(211, 211, 211); color: blue; border-radius: 4px; font-size: 12px !important; background: url("../jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png") 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="color: black; display: block; padding: 0.4em 1em;">Status</span></a></span></p><div class="hiddable" id="vj_description" style="font-family: Simsun;font-size:14px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: "Times New Roman", Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-size: 12pt;">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 <center>10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000</center>If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be <center>1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.</center></div></div></div><div class="hiddable" id="vj_input" style="font-family: Simsun;font-size:14px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: "Times New Roman", Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-size: 12pt;">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.</div></div></div><div class="hiddable" id="vj_output" style="font-family: Simsun;font-size:14px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Output</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: "Times New Roman", Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="ptx" lang="en-US" style="font-size: 12pt;">Output must contain a single integer - the minimal score.</div></div></div><div class="hiddable" id="vj_sampleInput" style="font-family: Simsun;font-size:14px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Sample Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: "Times New Roman", Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><pre class="sio" style="white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New", Courier, monospace; font-size: 12pt;">610 1 50 50 20 5

Sample Output

3650

Source

Northeastern Europe 2001, Far-Eastern Subregion


0 0
原创粉丝点击