Simple DP "p3399" Silk Road

Source: Internet
Author: User

Background

Zhang Qian in 138 BC had gone through the difficult and dangerous to the western regions. The friendship between the Han Dynasty and the western countries was strengthened. Since then, a team of camel caravan on the long road of trade, they crossed the mountains, the Chinese advanced technology to Central Asia, West Asia and Europe, where the spices, it stumbles sent into our country. Whenever people stare at the desolate desert solitary smoke, all caused the past business, cultural prosperity of reverie ...

Description

Small hamster with goods, from China to rest, the Silk Road including the beginning and end of the total n+1 city, No. 0 City is the beginning of Changan, N city is the end of Baghdad. Requires no more than m days to reach the end point. The time of day can be from one city to the next city in succession. From the I-1 city to i the city distance is di.

As we all know, continuous travel is very hard, so the hamster can be in a city when there are the following options:

  • Move: Down a city
  • Rest: Stay in the Old City and not move

The desert weather is changeable, and when the weather is bad, there are many difficulties ahead. We recorded the bad climatic value of M-day's J (1<=j<=m) day as CJ. Moving from I-1 City to I city on the first day of J, it takes DI*CJ fatigue.

However, the hamster still has the option to avoid the more severe weather, rest will not consume fatigue value. Now he wants to know how much fatigue the whole trip will cost.

Input

First row 2 integers n,m

Consecutive n rows per line an integer DJ

Continuous m line one integer per line CJ

Output

An integer representing the minimum fatigue level

It is foolish to say that I have just started. qwq

Set \ (f[i][j]\) represents the minimum fatigue value of the consumption of the ( i\) City of the first ( j\) Day of arrival.

Then we can consider resting in situ, or consider moving from the previous city to the city in j\ ( i\) days.

You can write the state transition equation
\[f[i][j]=min (F[i][j-1],f[i-1][j-1]+d[i] \times c[j]) \]
Just do it, remember to (f[0][i]\) assigned the initial value, do not forget \ (f[0][0]\)!

代码

#include<cstdio>#include<cctype>#include<iostream>#include<cstring>#define R registerusing namespace std;inline void in(int &x){    int f=1;x=0;char s=getchar();    while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}    while(isdigit(s)){x=x*10+s-'0';s=getchar();}    x*=f;}int n,m,f[1008][1008];int D[1008],C[1008],ans=2147483647LL;int main(){    in(n),in(m);memset(f,127,sizeof f);    for(R int i=1;i<=n;i++)in(D[i]);    for(R int i=1;i<=m;i++)in(C[i]),f[0][i]=0;f[0][0]=0;    for(R int i=1;i<=n;i++)        for(R int j=i;j<=m;j++)            f[i][j]=min(f[i][j-1],f[i-1][j-1]+D[i]*C[j]);    for(R int i=n;i<=m;i++)ans=min(ans,f[n][i]);    printf("%d",ans);}

Simple DP "p3399" Silk Road

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.