Topic Link: Click to open the link
Bare 01 Backpack, the value of this problem is volume ... Print path: Not much to say a one-dimensional did not read. The last two-dimensional
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 10100#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 1000000007#define pp pair<int,int> #define ull unsigned long longusing nam Espace std;int n,n,w[25],dp[25][maxn],ans[25];void input () {memset (dp,0,sizeof (DP)); for (int i=1;i<=n;i++) {scanf ( "%d", &w[i]); for (int j=0;j<=n;j++) {dp[i][j]= (i==1?0:dp[i-1][j]); if (J>=w[i]) Dp[i][j]=max (Dp[i-1][j],dp[i-1][j-w[i]]+w[i]);}} void output () {int p=0,c=dp[n][n];for (int i=n;i>=1;i--) {if (Dp[i][c]!=dp[i-1][c]) {ans[p++]=i;c-=w[i];}} for (int i=p-1;i>=0;i--) printf ("%d", W[ans[i]]);p rintf ("sum:%d\n", Dp[n][n]);} int main () {while (scanf ("%d%d", &n,&n)!=eof) {input (); output ();} return 0;}
Uva 642-CD (0-1 backpack + print path)