UVa 624 CD 01 Backpack Record Path
Descirption: NULL
"Give you a CD of the past to listen to our love at that time."
Simple 01 Backpack + path Recording.
Purple remembrance of the Great God:
can open another array a[i][j], when the dp[i][j] in the dynamic transfer of records, take the item I is a[i][j]=0, otherwise a[i][j]=1; Then the array a[i][j] can be traced back
Gives two ways to print: Recursive and non-recursive
void print (int A, b) { if (!a) if (Vis[a][b]) {print (a -1 , b- W[a]); printf ( %d " else print (A-1
for (int0; I--) { if(Vis[i][j]) { printf (" ", W[i]); J-= w[i]; } }
Slag Code:
1#include <iostream>2#include <string.h>3#include <math.h>4#include <algorithm>5#include <stdlib.h>6#include <stdio.h>7 #definell Long Long8 9 using namespacestd;Ten intf[10005], w[ -], N, m; One BOOLvis[ -][10005]; A voidPrintintAintb) { - if(!a)return; - if(Vis[a][b]) { thePrint (A-1, B-W[a]); -printf"%d", W[a]); - } - ElsePrint (A-1, b); + } - intMain () + { A while(~SCANF ("%d%d", &m, &n) && m +N) { atMemset (F,0,sizeof(f)); -memset (Vis,0,sizeof(Vis)); - for(inti =1; I <= N; i + +){ -scanf"%d", &w[i]); - } - for(inti =1; I <= N; i + +){ in for(intj = m; J >= W[i]; J--){ - if(F[j] <= F[j-w[i]] +W[i]) { toF[J] = F[j-w[i]] +W[i]; +VIS[I][J] =1; - } the } * } $ print (n, m);Panax Notoginseng //for (int i = n,j = m; i > 0; I-) { - //if (Vis[i][j]) { the //printf ("%d", w[i]); J-= W[i]; + // } A // } theprintf"sum:%d\n", F[m]); + } - return 0; $}
UVa 624 CD
Nailed IT.
-----------------------
UVa 624 CD