作者特别喜欢写这种DP的题解

这是一道DP中的背包问题

上代码:

#include<bits/stdc++.h>//万能头文件 
using namespace std;
int f[1010],w[110],c[110];
int main()
{
	int t,m;
	scanf("%d %d",&t,&m);
	for(int i=1;i<=m;++i)
		scanf("%d %d",&w[i],&c[i]);//每种药的摘采时间和价值 
	for(int i=1;i<=m;++i)//循环每一种植物 
	for(int v=t;v>=w[i];--v)//f[v]表示时间不超过v的最大价值 
	if(f[v]<f[v-w[i]]+c[i])
		f[v]=f[v-w[i]]+c[i];//判断将草药放入背包是否更优解 
	printf("%d\n",f[t]);//输出储存在f[t]里的最优值 
	return 0;
}

希望对大家有帮助!