DP专题
作者:互联网
Find More Coins (30)
Link
01背包问题
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <cmath>
using namespace std;
int n,m;
int w[10001];
int dp[101];
bool path[10001][101];
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%d",&w[i]);
sort(w+1,w+1+n,greater<int>());
for(int i=1;i<=n;++i)
for(int j=m;j>=w[i];--j)
if(dp[j-w[i]]+w[i]>=dp[j]){
path[i][j]=true;
dp[j]=dp[j-w[i]]+w[i];
}
if(dp[m]!=m) printf("No Solution\n");
else{
int i=n,j=m;
while(1){
if(path[i][j]){
printf("%d",w[i]);
j-=w[i];
if(j!=0) printf(" ");
}
i--;
if(i==0||j==0) break;
}
}
return 0;
}
标签:专题,10001,int,DP,printf,path,include,dp 来源: https://www.cnblogs.com/preccrep/p/16403887.html