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 n,m;
    while(cin>>m)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        cin>>value[i];
        memset(dp,0,sizeof(dp));
        memset(s,0,sizeof(s));
        for(int i=1;i<=n;i++)
         for(int j=m;j>=value[i];j--)
          if(dp[j]<=dp[j-value[i]]+value[i])
          {
              dp[j]=dp[j-value[i]]+value[i];
              s[i][j]=1;
          }
        for(int i=n,j=m;i>=1;i--)
        {
            if(s[i][j])
            {
                cout<<value[i]<<" ";
                j=j-value[i];
            }
        }
        cout<<"sum:"<<dp[m]<<endl;
    }
    return 0;
}
时间: 2024-12-22 01:31:21

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 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 >

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

UVA 624 CD (01背包 带路径)

题意 输入两个数 len,n 表示长度和个数,接下来输入n个数, 表示每一个的长度, 求这n个数能够组成的不超过len的最大长度,并输出这些数. 分析:01背包,dp数组非0表示可以组成的数,dp数组用来记录路径 #include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack> #i

UVa CD 0-1背包且打印路径

就是简单的0-1背包问题,不过没有具体的效益值,隐含的效益值就是剩余背包的容量.因为要输出具体选择了那些track(也就是物品),所以采用序偶的方法.其实0-1背包的解画在坐标轴上就是一个分段函数,所谓序偶就是那些跃迁的节点.但是这道题略有不同,第0阶段的初始序偶不是(0,0),而是(0,N).序偶的第一个参数表示容量,第二个参数表示背包的剩余容量.当由前一阶段的序偶得到新序偶,并且将两者合并的时候.应当按照序偶的第一个参数从小到大进行合并,如果下一个要合并的序偶的剩余容量大于当前最后一个以合并

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

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

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

Uva 10130-SuperSale(0-1背包)

题目链接:点击打开链接 裸的0-1背包 ..只不过相当于有多个背包,因为这些背包互相独立,求和相加就ok #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio> #in