link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565

用一个二维数组g[i][v]表示:当状态转移到v的时候,第i个物品是不是用到,如果用到标记1,否则标记0.

输出路径的时候,注意,从物品编号0一直到n-1.如果某个物品被用到了,g[i][v]里面的v,就要减去这个物品的体积,然后继续往下找.

 /*
* =====================================================================================
* Filename: cd.cpp
* Created: 04/08/2013 15:21:34
* Author: liuxueyang (lxy), 1459917536@qq.com
* Organization: Hunan University
*
* =====================================================================================
*/ /*
ID: zypz4571
LANG: C++
TASK: cd.cpp
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <list>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LL long long
const double eps=1e-;
using namespace std;
int V, n, c[], f[], g[][];
int main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
while (cin>>V) {
cin>>n;
for (int i = ; i < n; ++i) cin>>c[i];
memset(f,,sizeof(f));
memset(g,,sizeof(g));
for (int i = n-; i>=; --i) {
for (int v = V; v>=c[i]; --v) {
if (f[v] <= f[v-c[i]]+c[i]) {
f[v] = f[v-c[i]]+c[i]; g[i][v] = ;
} else g[i][v] = ;
}
}
for (int i = , j = V; i < n; ++i) {
if (g[i][j]) {
cout<<c[i]<<' ';
j-=c[i];
}
}
cout<<"sum:"<<f[V]<<endl;
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

再一次表示vim比codeblocks好用多了,囧

uva624 CD   01背包+输出最优解的更多相关文章

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

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

  2. UVA--624 CD(01背包+路径输出)

    题目http://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. Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)

    题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...

  5. vijos 1071 01背包+输出路径

    描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是 ...

  6. CD(01背包)

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

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

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

  8. c4 L3-001 找零钱 (简单01背包-输出最小字典序解(用vector保存当前最优解))

    #include <iostream> #include <algorithm> #include <vector> #include <cstdio> ...

  9. UVA 624CD(01背包输出 + 输出路径)

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

随机推荐

  1. 判断数据库内容,在页面显示自定义数据case when

    判断数据库内容,在页面显示自定义数据 case when...then ...else...end 比如:数据库内容是这样: 通过sql语句判断,数据库的name字段,内容是月桂的,显示嫦娥,其他的显 ...

  2. mysql.default_socket 和 mysqli.default_socket

    php.ini 里面mysql.default_socket 和 mysqli.default_socket 设置必须要和mysql里面的值一样,否则连接mysql会提示错误

  3. 房子里的K2 BPM业务流程管理

    房…子这件事上,尴尬实在太多. ಥ_ಥ 职场新人,租房很尴尬: 未婚男女,婚房很尴尬: 有下一代的,学区房很尴尬: 耄耋之年,养老房很尴尬... ▽ 甭管买房.租房.装修.设计,关于房子的尴尬事,三天 ...

  4. chrom,firefox,ie不能上网,百度浏览器却可以。。。

    chrome和ie提示DNS查找失败,但是百度浏览器没任何问题,这是什么情况... 尝试很多方法后无用,命令行执行很多命令,无用, 试一下阿里的 DNS: 首选:223.5.5.5备用:223.6.6 ...

  5. The ShortCuts in the ADT (to be continued)

    1. automatically add all the namespace which need to be include in the class. ctrl+shift+o

  6. Interview----判断整数序列是否是二叉搜索树的后序遍历结果

    题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果:   ...

  7. Map写入的顺序 输出地顺序ZT

    偶然间 发现hashmap遍历的结果不是放入的顺序 为了项目某个功能更人性话 思考了半天还是不知道如何下手 因为有种种条件限制 后来 无意中发现 java.util.LinkedHashMap< ...

  8. Java custom annotations

    Custom annotation definition is similar as Interface, just with @ in front. Annotation interface its ...

  9. Android白天/夜间模式Day/Night Mode标准原生SDK实现

     Android白天/夜间模式Day/Night Mode标准原生SDK实现 章节A:Android实现白天/夜间模式主要控制器在于UiModeManager,UiModeManager是Andr ...

  10. c#使用正则表达式替换html标签

    /// <summary> /// 去除HTML标记(用正则彻底去除HTML\CSS\script代码 ) /// </summary> /// <param name= ...