题目传送门
解题思路:
f[i][j]表示第j天到第i个城市消耗的最小体力值,对于每种状态,我们可能从两种状态转移过来:
1.我们可以选择前一天没动,即f[i][j-1]
2.可以选择前一天动了,即f[i-1][j-1]
还有,第1座城市比较特殊,所以要单独处理一下.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,m,a[1001],b[1001],f[1001][1001],ans = 1999999999; 8 9 inline int min(int s,int d) { 10 if(s < d) return s; 11 return d; 12 } 13 14 int main() { 15 memset(f,0x3f3f3f,sizeof(f)); 16 scanf("%d%d",&n,&m); 17 for(int i = 1;i <= n; i++) 18 scanf("%d",&a[i]); 19 for(int i = 1;i <= m; i++) 20 scanf("%d",&b[i]); 21 f[1][1] = b[1] * a[1]; 22 for(int i = 1;i <= m; i++) f[1][i] = min(f[1][i-1],a[1] * b[i]); 23 for(int i = 2;i <= n; i++) 24 for(int j = i;j <= m; j++) 25 f[i][j] = min(f[i][j-1],f[i-1][j-1] + a[i] * b[j]); 26 for(int i = n;i <= m; i++) 27 ans = min(f[n][i],ans); 28 printf("%d",ans); 29 return 0; 30 }