求最早什麼時候出來?
把高度看做揹包的容量,能量看做物品的價值。特判一下在使用這個“物品”時,能量是否大於等於現在的時間。注意讀入的數據還要按照時間排一下序。一旦高度達到,立刻輸出時間。如果一直沒有找到可行的方案,直接輸出dp[0]
代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+10;
int D,G;
int dp[maxn];
struct hh
{
int t,v,w;
}e[maxn];
bool cmp(hh x,hh y)
{
return x.t<y.t;
}
int main()
{
scanf("%d%d",&D,&G);
for(int i=1;i<=G;++i) scanf("%d%d%d",&e[i].t,&e[i].w,&e[i].v);
sort(e+1,e+G+1,cmp);
dp[0]=10;
for(int i=1;i<=G;++i)
for(int j=D;j>=0;--j)
if(dp[j]>=e[i].t)
{
if(j+e[i].v>=D)
{
printf("%d",e[i].t);
return 0;
}
dp[j+e[i].v]=max(dp[j+e[i].v],dp[j]);
dp[j]+=e[i].w;
}
printf("%d",dp[0]);
return 0;
}