Uva 642-CD (0-1 backpack + print path)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.