uva 624 CD 01背包打印路径

// 集训最终開始了。来到水题先

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int a[23];
int d[23][100000];
int flag[23];
int W,n;

void init(){
    cin >> n;
    for (int i=1;i<=n;i++)
        cin >> a[i];
    memset(flag,0,sizeof(flag));
    memset(d,0,sizeof(d));
}

void print(int x,int y){
    if (y<=0||x<1)  return ;
    //cout << x << " " << y << endl;
    if (d[x][y]==d[x-1][y]){
        flag[x] = 0;
        print(x-1,y);
        //return ;
    }else if (d[x][y]==d[x-1][y-a[x]] + a[x]){
        flag[x] = 1;
        //cout << a[x] << " ";
        print(x-1,y-a[x]);
        //return ;
    }
}

void solve(){
    for (int i=1;i<=n;i++){
        for (int j=W;j>=0;j--){
            d[i][j] = d[i-1][j];
            if (j>=a[i])
                d[i][j] = max(d[i-1][j],d[i-1][j-a[i]] + a[i]);
        }
    }

    print(n,W);

    for (int i=1;i<=n;i++)
        if (flag[i])
            printf("%d ",a[i]);
    printf("sum:%d\n",d[n][W]);
}

int main(){
    //freopen("1.txt","r",stdin);
    while(cin >> W){
        init();
        solve();
    }
}
时间: 2024-10-02 02:34:56

uva 624 CD 01背包打印路径的相关文章

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

Description You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most o

Uva 642-CD(0-1背包+打印路径)

题目链接:点击打开链接 裸01背包 ,此题中 价值即体积... 打印路径..不多说了 一维的没看懂..上个二维的 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio>

UVA 624 (0 1背包 + 打印路径)

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<algorithm> #define N 1010 using namespace std; int dp[N], path[N][N], w[N]; int main() { int v, n; while(~scanf("%d", &v)) { sca

UVA 624 CD (01背包)

//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: #include <iostream> #include <cstring> #include <algorithm> using namespace std; int value[30],dp[10001],s[30][10001]; int main() { int

0-1背包打印路径(递归和非递归版本)

简单的0-1背包打印路径问题,我们可以记录一个p[][]数组来判断,当前物品是否被选中,最后按照记录输出,注意是逆序. #include<stdio.h> #include<string.h> int main() { int a[25],p[25][10005],i,j,n,m,s[10005]; while(scanf("%d%d",&m,&n)!=EOF){ for(i=1;i<=n;i++) scanf("%d"

UVA 624 CD

CD Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 62464-bit integer IO format: %lld      Java class name: Main You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music

UVA624(01背包记录路径)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565 01背包打印路径. #include <cstdio> #include <cstring> using namespace std; const int MAXN=10005; int n,W; int w[30]; int dp[MAXN]; in

uva 624 CD (01背包)

uva 624 CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most ou

UVA 624 CD 记录路径DP

开一个数组p 若dp[i-1][j]<dp[i-1][j-a[i]]+a[i]时就记录下p[j]=a[i];表示此时放进一个轨道 递归输出p #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #in