题目传送门
解题思路:
f[i]表示把i只羊渡河的最少时间,则本次可能带1~i只羊过河,然后转移方程就出来了.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,m,a[2501],sum[2501],f[2501]; 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 sum[i] = sum[i-1] + a[i]; 20 } 21 f[0] = 0; 22 for(int i = 1;i <= n; i++) { 23 f[i] = sum[i]; 24 for(int j = 1;j < i; j++) 25 f[i] = min(f[i],f[j] + sum[i-j] + m); 26 f[i] += m; 27 } 28 printf("%d",f[n]); 29 return 0; 30 }