f[i] 表示送前 i 头牛过去再回来的最短时间
f[i] = min(f[i], f[j] + sum[i - j] + m) (0 <= j < i)
——代码
1 #include <cstdio> 2 #include <iostream> 3 4 const int MAXN = 2501, INF = 12345678; 5 int n, m; 6 int sum[MAXN], f[MAXN]; 7 8 inline long long read() 9 { 10 long long x = 0, f = 1; 11 char ch = getchar(); 12 for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1; 13 for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; 14 return x * f; 15 } 16 17 inline int min(int x, int y) 18 { 19 return x < y ? x : y; 20 } 21 22 int main() 23 { 24 int i, j, x; 25 n = read(); 26 m = read(); 27 sum[0] = m; 28 for(i = 1; i <= n; i++) 29 { 30 x = read(); 31 sum[i] = x + sum[i - 1]; 32 } 33 for(i = 1; i <= n; i++) 34 { 35 f[i] = INF; 36 for(j = 0; j < i; j++) f[i] = min(f[i], f[j] + sum[i - j] + m); 37 } 38 printf("%d ", f[n] - m); 39 return 0; 40 }