uva624 CD 01背包+输出最优解
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背包+输出最优解的更多相关文章
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- UVA--624 CD(01背包+路径输出)
题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 624 ---CD 01背包路径输出
DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...
- Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)
题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...
- vijos 1071 01背包+输出路径
描述 过年的时候,大人们最喜欢的活动,就是打牌了.xiaomengxian不会打牌,只好坐在一边看着. 这天,正当一群人打牌打得起劲的时候,突然有人喊道:“这副牌少了几张!”众人一数,果然是少了.于是 ...
- CD(01背包)
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- c4 L3-001 找零钱 (简单01背包-输出最小字典序解(用vector保存当前最优解))
#include <iostream> #include <algorithm> #include <vector> #include <cstdio> ...
- UVA 624CD(01背包输出 + 输出路径)
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
随机推荐
- js用正则表达式控制价格输入
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- ZOJ3675:Trim the Nails
Robert is clipping his fingernails. But the nail clipper is old and the edge of the nail clipper is ...
- "琳琅满屋"调查问卷 心得体会及结果分析
·关于心得体会 当时小组提出这个校园二手交易市场的时候,就确定了对象范围,仅仅是面向在校大学生,而且在我们之前就已经有了很多成功的商品交易的例子可以让我们去借鉴,再加上我们或多或少的有过网 ...
- FCKEditor文件上传提示信息的汉化
FCKEditor文件上传提示信息的汉化在FCKeditor中,虽然可以自动监测客户端语言,但是仍有小部分信息未能得到汉化.例如上传图片.Flash时,上传成功和上传失败的对话框提示信息均为英文,只要 ...
- Unity安卓连接profile调试
通过USB ADB 1.从Unity中Export Android 工程的时候一定要勾选 Development Build,autoconnect profiler 2.cmd进入adb的目录(打开 ...
- [网络技术][转]PPTP协议解析
PPTP协议大体上可以分为两部分:控制层连接和隧道,下面简要介绍两部分的功能.如果要详细了解PPTP协议请阅读RFC文档. 一. Control Connection Protol 控制层连接是基于T ...
- Oracle Data Integrator与OWB的集成及迁移
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- (转)Tomcat的目录结构
原文:http://hi.baidu.com/qinyougen/item/beeb506abb3e1d08a1cf0ffb Tomcat的目录结构 一.TOMCAT的目录结构 /bin:存放wind ...
- php大力力 [013节]mySQL数据库乱码问题我还没解决
<?php echo"测试<br>"; $sql_connection = mysql_connect("localhost","e ...
- <xliff:g>标签
摘要: 这是Android4.3Mms源码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sms ...