C++动态规划-01背包

采药

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int t,m;//T:总时间 m:物品数数量
int dp[1005];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>t>>m;
	for(int i=1; i<=m; i++)
	{
		int w,v;
		cin>>w>>v;
		for(int j=t; j>=w; j--) dp[j]=max(dp[j],dp[j-w]+v);
	}
	cout<<dp[t];
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int t,m;//T:总时间 m:物品数数量

int dp1005;

int main()

{

ios::sync_with_stdio(0);

cin.tie(0);cout.tie(0);

cin>>t>>m;

for(int i=1; i<=m; i++)

{

int w,v;

cin>>w>>v;

for(int j=t; j>=w; j--) dpj=max(dpj,dpj-w+v);

}

cout<<dpt;

return 0;

}

开心的金明

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int t,m;
int dp[100005];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>t>>m;
	for(int i=1; i<=m; i++)
	{
		int money,zh;
		cin>>money>>zh;
		int w=money,v=money*zh;
		for(int j=t; j>=w; j--) dp[j]=max(dp[j],dp[j-w]+v);
	}
	cout<<dp[t];
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int t,m;

int dp100005;

int main()

{

ios::sync_with_stdio(0);

cin.tie(0);cout.tie(0);

cin>>t>>m;

for(int i=1; i<=m; i++)

{

int money,zh;

cin>>money>>zh;

int w=money,v=money*zh;

for(int j=t; j>=w; j--) dpj=max(dpj,dpj-w+v);

}

cout<<dpt;

return 0;

}

花园采药

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int T;
int n,m;
int dp[10005];
int v[205],w[205];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>T;
	for(int k=1; k<=T; k++)
	{
		cin>>n>>m;
		memset(dp,0,sizeof(dp));
		for(int i=1; i<=n; i++) cin>>v[i];
		for(int i=1; i<=n; i++)
		{
			cin>>w[i];
			for(int j=m; j>=w[i]; j--) dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
		}
		//for(int i=1; i<=m; i++) cout<<dp[i]<<" ";
		//cout<<'\n'; 
		cout<<dp[m]<<'\n';
	}
	return 0;
}
/*
2
5 10
1 2 3 4 5
5 4 3 2 1
5 10
1 2 3 4 5
5 1 5 4 3

*/

#include <bits/stdc++.h>

using namespace std;

int T;

int n,m;

int dp10005;

int v205,w205;

int main()

{

ios::sync_with_stdio(0);

cin.tie(0);cout.tie(0);

cin>>T;

for(int k=1; k<=T; k++)

{

cin>>n>>m;

memset(dp,0,sizeof(dp));

for(int i=1; i<=n; i++) cin>>vi;

for(int i=1; i<=n; i++)

{

cin>>wi;

for(int j=m; j>=wi; j--) dpj=max(dpj,dpj-w\[i]+vi);

}

//for(int i=1; i<=m; i++) cout<<dpi<<" ";

//cout<<'\n';

cout<<dpm<<'\n';

}

return 0;

}

/*

2

5 10

1 2 3 4 5

5 4 3 2 1

5 10

1 2 3 4 5

5 1 5 4 3

*/

装箱问题

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int n,m;
int dp[100005];
int v[205],w[205];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>m>>n;
	for(int i=1; i<=n; i++)
	{
		cin>>w[i];
		for(int j=m; j>=w[i]; j--) dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
	}
	//for(int i=1; i<=m; i++) cout<<dp[i]<<" ";
	//cout<<'\n'; 
	cout<<m-dp[m]<<'\n';
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int n,m;

int dp100005;

int v205,w205;

int main()

{

ios::sync_with_stdio(0);

cin.tie(0);cout.tie(0);

cin>>m>>n;

for(int i=1; i<=n; i++)

{

cin>>wi;

for(int j=m; j>=wi; j--) dpj=max(dpj,dpj-w\[i]+wi);

}

//for(int i=1; i<=m; i++) cout<<dpi<<" ";

//cout<<'\n';

cout<<m-dpm<<'\n';

return 0;

}

数字组合

cpp 复制代码
#include <bits/stdc++.h> 
using namespace std;
int n,m; 
int a[105];
int dp[10005];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=n; i++) cin>>a[i];
	dp[0]=1;
	for(int i=1; i<=n; i++)
	{
		int now=a[i];
		for(int j=m; j>=now; j--) dp[j]+=dp[j-now];
	}
//	for(int i=1; i<=m; i++) cout<<dp[i]<<" ";
//	cout<<'\n';
	cout<<dp[m];
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int n,m;

int a105;

int dp10005;

int main()

{

cin>>n>>m;

for(int i=1; i<=n; i++) cin>>ai;

dp0=1;

for(int i=1; i<=n; i++)

{

int now=ai;

for(int j=m; j>=now; j--) dpj+=dpj-now;

}

// for(int i=1; i<=m; i++) cout<<dpi<<" ";

// cout<<'\n';

cout<<dpm;

return 0;

}

淘淘的盒子

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int n,z;
int w[105][3];
int v[105][3];
int s[10005];
int t1[10005];
int t2[10005];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>z;
	for(int i=1; i<=n; i++)
	{
		cin>>w[i][1]>>v[i][1];
		cin>>w[i][2]>>v[i][2];
		for(int j=z; j>=w[i][1]; j--) t1[j]=max(s[j],s[j-w[i][1]]+v[i][1]);
		for(int j=z; j>=w[i][2]; j--) t2[j]=max(s[j],s[j-w[i][2]]+v[i][2]);
		for(int j=z; j>=min(w[i][1],w[i][2]); j--) s[j]=max(t1[j],t2[j]);
	}
	cout<<s[z];
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int n,z;

int w1053;

int v1053;

int s10005;

int t110005;

int t210005;

int main()

{

ios::sync_with_stdio(0);

cin.tie(0);cout.tie(0);

cin>>n>>z;

for(int i=1; i<=n; i++)

{

cin>>wi1>>vi1;

cin>>wi2>>vi2;

for(int j=z; j>=wi1; j--) t1j=max(sj,sj-w\[i1]+vi1);

for(int j=z; j>=wi2; j--) t2j=max(sj,sj-w\[i2]+vi2);

for(int j=z; j>=min(wi1,wi2); j--) sj=max(t1j,t2j);

}

cout<<sz;

return 0;

}

金明的预算方案

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int n,m,v1,p1,q1;
int v[105][3],p[105][3];
int s[105][200005];
int main()
{
	cin>>n>>m;
	for(int i=1; i<=m; i++)
	{
		cin>>v1>>p1>>q1;
		if(q1)
		{
			if(v[q1][1])
			{
				v[q1][2]=v1;
				p[q1][2]=p1;
			}
			else
			{
				v[q1][1]=v1;
				p[q1][1]=p1;
			}
		}
		else
		{
			v[i][0]=v1;
			p[i][0]=p1;
		}
	}
	for(int i=1; i<=m; i++)
	{
		for(int j=0; j<=n; j++)
		{
            s[i][j]=s[i-1][j];
			if(v[i][0]<=j)
			{
				int nee=v[i][0]*p[i][0];
				s[i][j]=max(s[i][j],s[i-1][j-v[i][0]]+nee);
			}
			if(v[i][0]+v[i][1]<=j)
			{
				int ne1=v[i][1]*p[i][1];
				int ne2=v[i][0]*p[i][0];
				s[i][j]=max(s[i][j],s[i-1][j-v[i][0]-v[i][1]]+ne1+ne2);
			}
			if(v[i][0]+v[i][2]<=j)
			{
				int ne1=v[i][2]*p[i][2];
				int ne2=v[i][0]*p[i][0];
				s[i][j]=max(s[i][j],s[i-1][j-v[i][0]-v[i][2]]+ne1+ne2);
			}
			if(v[i][0]+v[i][1]+v[i][2]<=j)
			{
				int ne1=v[i][0]*p[i][0];
				int ne2=v[i][1]*p[i][1];
				int ne3=v[i][2]*p[i][2];
				s[i][j]=max(s[i][j],s[i-1][j-v[i][0]-v[i][1]-v[i][2]]+ne1+ne2+ne3);
			}
		}
	}
	cout<<s[m][n];
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

int n,m,v1,p1,q1;

int v1053,p1053;

int s105200005;

int main()

{

cin>>n>>m;

for(int i=1; i<=m; i++)

{

cin>>v1>>p1>>q1;

if(q1)

{

if(vq11)

{

vq12=v1;

pq12=p1;

}

else

{

vq11=v1;

pq11=p1;

}

}

else

{

vi0=v1;

pi0=p1;

}

}

for(int i=1; i<=m; i++)

{

for(int j=0; j<=n; j++)

{

sij=si-1j;

if(vi0<=j)

{

int nee=vi0*pi0;

sij=max(sij,si-1j-v\[i0]+nee);

}

if(vi0+vi1<=j)

{

int ne1=vi1*pi1;

int ne2=vi0*pi0;

sij=max(sij,si-1j-v\[i0-vi1]+ne1+ne2);

}

if(vi0+vi2<=j)

{

int ne1=vi2*pi2;

int ne2=vi0*pi0;

sij=max(sij,si-1j-v\[i0-vi2]+ne1+ne2);

}

if(vi0+vi1+vi2<=j)

{

int ne1=vi0*pi0;

int ne2=vi1*pi1;

int ne3=vi2*pi2;

sij=max(sij,si-1j-v\[i0-vi1-vi2]+ne1+ne2+ne3);

}

}

}

cout<<smn;

return 0;

}

包包

未完待续--敬请期待

相关推荐
geminigoth1 天前
python入门三:字典、输入、while循环
开发语言·python
牛油果子哥q1 天前
【C++ this指针】C++ this指针深度精讲:this底层本质、存储位置、调用机制、const this指针、空指针调用、面试坑点与工程实战
开发语言·c++·面试
胡萝卜的兔1 天前
go使用voidint实现版本切换
开发语言·chrome·golang
坚果派·白晓明1 天前
[鸿蒙PC三方库移植适配] 使用 AtomCode + Skills 自动完成spdlog鸿蒙化适配
c++·华为·ai编程·harmonyos·skills·atomcode
小禹在努力1 天前
brpc1.15问题实录:bthread_worker_count取Node CPU,K8s缩容引发线程数超限
c++·微服务·rpc
Never_love_MCI!1 天前
洛谷P15799 [GESP202603 五级] 找数 题解
数据结构·c++·算法
思麟呀1 天前
C++11核心特性(二):constexpr
开发语言·c++
程序员二叉1 天前
【Java】String 全套高频面试题详解
java·开发语言·面试
阿里嘎多学长1 天前
2026-06-07 GitHub 热点项目精选
开发语言·程序员·github·代码托管
字节高级特工1 天前
C++11(三)终极指南:可变参数模板与包装器详解
java·开发语言·c++·后端