社区讨论

方向错了在努力也GG

灌水区参与者 3已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@lu3es0fn
此快照首次捕获于
2024/03/23 09:25
2 年前
此快照最后确认于
2024/03/23 11:21
2 年前
查看原帖
背包问题 自闭
搜索循环50多行 TLE
DP 23行AC
无语
不过...有没有大佬帮我剪枝一下啊~我的(搜索)代码写了2h不想就这么扔了啊 (完全背包问题)
CPP
#include<bits/stdc++.h>
using namespace std;
int m,n;
struct node
{
	int heavy,score;
}item[10000];
int max_sc=-1;
void dfs(int sum,int h,int step)
{
	if (h>m)return;
	if (step==n or h==m)
	{
		if (sum>max_sc)max_sc=sum;
		return;
	}
	for (int i=0;i<=floor((m-h)/(item[step].heavy));i++)
		dfs(sum+(item[step].score)*i,h+(item[step].heavy)*i,step+1);
}
int main()
{
	cin>>m>>n;
	for (int i=0;i<n;i++)
	{
		cin>>item[i].heavy>>item[i].score;
	}
	dfs(0,0,0);
	cout<<"max="<<max_sc;
	return 0;
}

回复

5 条回复,欢迎继续交流。

正在加载回复...