穿越隧道
二维dp
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;
int n,m;
int lose[N],win[N],use[N];
ll f[N][N];
int main(){
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++) scanf("%d%d%d",&lose[i],&win[i],&use[i]);
for(int i = 1; i <= n; i++){
for(int j = 0; j <= m; j++){
if(j < use[i]){
f[i][j] = f[i-1][j] + lose[i];
}
else{
f[i][j] = max(f[i-1][j] + lose[i], f[i-1][j - use[i]] + win[i]);
}
}
}
printf("%lld\n",5ll*f[n][m]);
return 0;
}
一维dp(优化)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;
int n,m;
int lose[N],win[N],use[N];
ll f[N];
int main(){
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++) scanf("%d%d%d",&lose[i],&win[i],&use[i]);
for(int i = 1; i <= n; i++){
for(int j = m; j >= 0; j--){
if(j >= use[i]){
f[j] = max(f[j] + lose[i], f[j - use[i]] + win[i]);
}
else{
f[j] = f[j] + lose[i];
}
}
}
printf("%lld\n",5ll*f[m]);
return 0;
}