题目传送门
解题思路:
f[i][j]表示前i辆车走了j个人的最小价钱。
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,m,d,s,k,ans; 8 int t[101],c[101],f[101][101]; 9 10 inline int min(int a,int b) { 11 if(a >= b) return b; 12 return a; 13 } 14 15 int main() { 16 scanf("%d%d%d%d",&n,&k,&d,&s); 17 memset(f,0x3f3f3f,sizeof(f)); 18 for(int i = 1;i <= k; i++) { 19 scanf("%d%d",&t[i],&c[i]); 20 if(t[i] > s && m == 0) m = i - 1;//如果在s之后,就不管了 21 } 22 if(m == 0) m = k; 23 f[0][0] = 0; 24 for(int i = 1;i <= m; i++) 25 for(int j = 0;j <= n; j++) { 26 f[i][j] = f[i-1][j]; 27 for(int k = 0;k <= min(c[i],j); k++) 28 f[i][j] = min(f[i][j],f[i-1][j-k] + k * t[i] + d); 29 } 30 if(f[m][n] > 1000000000) printf("impossible"); 31 else 32 printf("%d",f[m][n]); 33 return 0; 34 }