#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))
{
scanf("%d", &n);
memset(dp, , sizeof(dp));
memset(path, , sizeof(path));
for(int i = ; i <= n ;i++)
scanf("%d", &w[i]);
for(int i = n ; i >= ; i--)
{
for(int j = v ; j >= w[i] ; j--)
{
if(dp[j] <= dp[j - w[i]] + w[i])
{
dp[j] = dp[j - w[i]] + w[i];
path[i][j] = ;
}//path记录
}
}
int j = v;
for(int i = ; i <= n ; i++)
{
if(path[i][j])
{
printf("%d ", w[i]);
j -= w[i];
}
}//打印路径
printf("sum:%d\n", dp[v]);
}
return ;
}

UVA 624 (0 1背包 + 打印路径)的更多相关文章

  1. uva 624 CD 01背包打印路径

    // 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...

  2. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. UVA 624 ---CD 01背包路径输出

    DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...

  4. UVA624 CD,01背包+打印路径,好题!

    624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...

  5. UVA 1626 区间dp、打印路径

    uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...

  6. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

  7. (动态规划 01背包 打印路径) CD --UVA --624

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/G 每个CD的时间不超过 20没有哪个CD的时间是超过N ...

  8. UVA 624 CD (01背包)

    //路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...

  9. UVA624(01背包记录路径)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. ubuntu13.04云主机部署gitlab6.6

    GitLab 是何物? GitLab是 GitHub 的山寨版,GitLab几乎包含了GitHub的所有功能,还包含比较有特色的功能:Code Review,Wiki,Merge Requests,最 ...

  2. UVa 10498 Happiness! (线性规划)

    题意 将N种食品分给m个参赛选手,一个单位的某食品给某个选手一定满足度,每个选手有一个最大满足度.为了避免浪费,分给每一个选手的食品都不超越选手的满足度.已知的各种食品的单价,求最多可以花的钱. 思路 ...

  3. PHP Simple HTML DOM Parser Manual-php解析DOM

    PHP Simple HTML DOM Parser Manual http://www.lupaworld.com/doc-doc-api-770.html PHP Simple HTML DOM ...

  4. (三)用Normal Equation拟合Liner Regression模型

    继续考虑Liner Regression的问题,把它写成如下的矩阵形式,然后即可得到θ的Normal Equation. Normal Equation: θ=(XTX)-1XTy 当X可逆时,(XT ...

  5. 【appium】关于logcat

    SDK的文档要看 http://developer.android.com/tools/help/logcat.html http://developer.android.com/tools/debu ...

  6. Bootstrap-select:美化原生select

    官网:http://silviomoreto.github.io/bootstrap-select/ 1.下载zip 2.html代码 <select class="selectpic ...

  7. nginx upstream的分配方式

    1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况. 例 ...

  8. WebApp开发经验

    1.自适应屏幕 <meta name="viewport" id="viewport" content="width = device-widt ...

  9. MVC的路由

    MVC的路由包括以下几部分 路由名称,路由URL,路由的初始值,路由的约束,路由的命名空间 routes.MapRoute( name: "Default", url: " ...

  10. 用js完成blog项目

    //前台调用 var $ = function (args) { return new Base(args); } //基础库 function Base(args) { //创建一个数组,来保存获取 ...