题解里全是dp,对于橙是不是有点过了,亲测dfs效率也不低,开O2最长6ms(#5),否则最长也只有16ms(#5),附上本狗(我弱到根本打不出那个字)弱的代码,求加入题解:
#include <iostream>
#include <unordered_map>
int n,m,ans = -0x80000000;
struct things{int a,b,c;}a[1005];
std::unordered_map <int,bool> vis;//记录是否装过第k组物品
void dfs(int x,int worth,int heavy){
if (heavy > m) return;
ans = std::max(ans,worth);
if (x > n) return;
dfs(x + 1,worth,heavy);
if (!vis[a[x].c]){
vis[a[x].c] = true;
dfs(x + 1,worth + a[x].b,heavy + a[x].a);
vis[a[x].c] = false;
}
}//正在考虑是否选择第x项物品,以选择的物品总价值为worth,总重量为heavy
int main(){
std::ios::sync_with_stdio(0);
std::cin.tie(0),std::cout.tie(0);
std::cin >> m >> n;
for (int i = 1;i <= n;i++) std::cin >> a[i].a >> a[i].b >> a[i].c;
dfs(1,0,0);
std::cout << ans << '\n';
return 0;
}