背包玄关求助
  • 板块灌水区
  • 楼主__^浅笑.__
  • 当前回复3
  • 已保存回复3
  • 发布时间2025/1/13 17:57
  • 上次更新2025/1/13 22:19:59
查看原帖
背包玄关求助
1342002
__^浅笑.__楼主2025/1/13 17:57

题目链接:https://www.luogu.com.cn/problem/P2370

蒟蒻代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define re return
#define ew 5005
#define sz 10000005
const int MOD=1e9+7;
struct node{
	int w,v;
}a[sz];
int dp[ew][ew];
bool cmp(node x,node y){
	return x.v<x.w;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,p,s;
	cin>>n>>p>>s;
	for(int i=1;i<=n;i++){
		cin>>a[i].w>>a[i].v;
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<=s;j++){
			if(j>=a[i].w){
				dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i].w]+a[i].v);
			}
			else{
				dp[i][j]=dp[i-1][j];
			}
		}
	}
	int sum;
	sort(a+1,a+n+1,cmp);
	int num=0;
	for(int i=1;i<=n;i++){
		num+=a[i].v;
		if(num>=p){
			sum=a[i].w;
			break;
		}
	}
	if(dp[n][s]<p){
		cout<<"No Solution!";
	}
	else{
		cout<<sum;;
	}
	re 0;
}

2025/1/13 17:57
加载中...