# interestingLSY 的博客

### 题解 P2517 【[HAOI2010]订货】

posted on 2018-02-10 21:11:42 | under 题解 |

• 贪心
• 费用流
• dp

# 现在我要讲讲dp做法！

## 状态转移？

${\color{red}{ dp_{i,j} \ = \ min \left\{\ dp_{i-1,k}+(j+U_i-k)\cdot D_i\ +\ k \cdot m \right\} }}$ $(\ 0 \leq k \leq min(j+U_i,S)\ )$

## 别放弃啊咱们一起优化

$\color{red} dp_{i,j} \ = \ \color{green}min \left\{\ dp_{i-1,k}-k\cdot D_i\ +\ k \cdot m \right\}\color{red}\ +\ (j+U_i)\cdot D_i$ $(\ 0 \leq k \leq min(j+U_i,S)\ )$

$\color{blue}F_{i,k}\ = \ dp_{i-1,k}-k\cdot D_i\ +\ k \cdot m$

$\color{red}M_{i,j}\ = \ min\left\{ F_{i,z}\right\}\ (0 \leq z \leq j)$

$dp_{i,j}\ =\ \color{red}M_{i,j} \color{black}+(j+U_i)\cdot D_i$

.

.

$\color{red}M_{i,j}\ =\ min(M_{i,j-1},F_{i,j})$ $(j \neq 0)$

$\color{red}M_{i,j}\ =\ F_{i,j}$ $(j = 0)$

## 好了上代码！

#include <bits/stdc++.h>
#define INF (0x3f3f3f3f)
#define ll long long
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))
using namespace std;
#define Mymax(a,b) if(a<b) a = b;
#define Mymin(a,b) if(a>b) a = b;
#define il inline
#define rg register
#define For(i,j) for( rg int (i) = 1 ; (i) <= (j) ; (i)++ )
#define For0(i,j) for( rg int (i) = 0 ; (i) < (j) ; (i)++ )
#define Forx(i,j,k) for( rg int (i) = (j) ; (i) <= (k) ; (i)++ )
#define Forstep(i,j,k,st) for( rg int (i) = (j) ; (i) <= (k) ; (i) += (st) )
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 60
#define MAXS 10010
int n,m,s;
int u[MAXN], d[MAXN];

int dp[MAXN][MAXS];
int mintmp[MAXN][MAXS];
// dp[i][j] 表示第i个月,卖之后库存为j的最小花费

il int F( int i , int k ){
return dp[i-1][k] + k*m - k*d[i];
}
il void M( int i , int k ){
if( k == 0 ) mintmp[i][k] = F(i,k);
else mintmp[i][k] = min(mintmp[i][k-1],F(i,k));
}
il void Pre( int i ){
Forx(j,0,s)
M(i,j);
}

int main(){
Msn(dp,INF);
Msn(mintmp,INF);

scanf("%d%d%d",&n,&m,&s);
For(i,n) scanf("%d",&u[i]);
For(i,n) scanf("%d",&d[i]);

dp[0][0] = 0;
For(i,n){
Pre(i);
For0(j,s+1){
int klimit = min(j+u[i],s);
dp[i][j] = mintmp[i][klimit] + (j+u[i])*d[i];
}
}

int ans = dp[n][0];
printf("%d\n",ans);

return 0;
}