方向错了在努力也GG
  • 板块灌水区
  • 楼主gaoyangyang
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/3/23 09:24
  • 上次更新2024/3/23 11:21:11
查看原帖
方向错了在努力也GG
733470
gaoyangyang楼主2024/3/23 09:24

背包问题 自闭

搜索循环50多行 TLE

DP 23行AC

无语

不过...有没有大佬帮我剪枝一下啊~我的(搜索)代码写了2h不想就这么扔了啊 (完全背包问题)

#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;
}
2024/3/23 09:24
加载中...