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 out of tape space and have as short unused space as possible.
Assumptions:
• number of tracks on the CD does not exceed 20
• no track is longer than N minutes
• tracks do not repeat
• length of each track is expressed as an integer number
• N is also integer
Program should find the set of tracks which fills the tape best and print it in the same sequence as
the tracks are stored on the CD
Input
Any number of lines. Each one contains value N, (after space) number of tracks and durations of the
tracks. For example from first line in sample data: N = 5, number of tracks=3, first track lasts for 1
minute, second one 3 minutes, next one 4 minutes
Output
Set of tracks (and durations) which are the correct solutions and string ‘sum:’ and sum of duration
times.
Sample Input
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
 
Sample Output
1 4 sum:5
8 2 sum:10
10 5 4 sum:19
10 23 1 2 3 4 5 7 sum:55
4 10 12 9 8 2 sum:45
 
 
Tip:注意两次循环的方向是相反的。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int dp[],w[];
int path[][]; int main()
{
int m,n;
while(cin>>m>>n)
{
memset(dp,,sizeof(dp));
memset(path,,sizeof(path));
memset(w,,sizeof(w));
for(int i=; i<=n; i++)
cin>>w[i];
for(int i=n; i>=; i--)
{
for(int j=m; j>=w[i]; j--)
{
if(dp[j-w[i]]+w[i]>dp[j])
{
dp[j]=dp[j-w[i]]+w[i];
path[i][j]=;
}
}
}
for(int i=,j=m; i<=n; i++)
{
if(path[i][j]==)
{
cout<<w[i]<<' ';
j-=w[i];
}
}
cout<<"sum:"<<dp[m]<<endl;
}
return ;
}

uva624 01背包要求输出路径的更多相关文章

  1. PAT L3-001 凑零钱(01背包dp记录路径)

    韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...

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

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

  3. POJ-1015(背包变形+输出路径)

    Jury Compromise POJ-1015 推荐几个较好的介绍和理解:https://blog.csdn.net/lyy289065406/article/details/6671105 htt ...

  4. Codeforces Round #436 E. Fire(背包dp+输出路径)

    题意:失火了,有n个物品,每个物品有价值pi,必须在时间di前(小于di)被救,否则就要被烧毁.救某个物 品需要时间ti,问最多救回多少价值的物品,并输出救物品的顺序. Examples Input ...

  5. Re0:DP学习之路 01背包如何打印路径?

    伪代码 用二维数组记录,如果出现可以转移的dp那么记录bk[当前体积][装的物品]=1 输出的时候倒推,如果存在连通的边那么输出并且总共的体积减去输出的体积 代码(uva-624,目前wa不明所以,网 ...

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

    题目链接: https://vjudge.net/problem/UVA-624 题目大意: 这道题给定一个时间上限,然后一个数字N,后面跟着N首歌的时间长度,要我们 求在规定时间w内每首歌都要完整的 ...

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

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

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

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

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

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

随机推荐

  1. 将jquery和公共样式缓存到localStorage,可以减少Http请求,从而优化页面加载时间

    以下代码: //入口函数 if (window.localStorage) { initJs(); initCss("css", "/gfdzp201508257998/ ...

  2. log4j:WARN No appenders could be found for logger

    直接写我的解决办法: 在src下面新建file名为log4j.properties内容如下:# Configure logging for testing: optionally with log f ...

  3. 【工具】 原版完美激活 Flash builder 4.7 【非破解激活】

    此方法原理在于激活 FlashBuilder 4.7 而不是破解(靠修改文件,或改变版本号),所以此破解更加稳定! FlashBuilder 4.7 下载地址: 32bit:http://trials ...

  4. sqlserver insert 存储过程

    -- 根据表中数据生成insert语句的存储过程Create Proc proc_insert (@tablename varchar(256))  as                        ...

  5. LeetCode 171 Excel Sheet Column Number

    Problem: Given a column title as appear in an Excel sheet, return its corresponding column number. F ...

  6. SQL基本CRUD

    --已知Oracle的Scott用户中提供了三个测试数据库表 --名称分别为dept,emp,salgrade.使用SQL语言完成一下操作 --1,查询20号部门的所有员工信息: SELECT * F ...

  7. openssl/asn1.h file not found的解决方法

    iOS 引入支付宝 缺少 #include <openssl/asn1.h>  报错     解决方法: 在 Building Settings -> Search Paths -& ...

  8. 查询局域网内在线电脑IP

    COLOR 0A CLS @ECHO Off Title 查询局域网内在线电脑IP :send @ECHO off&setlocal enabledelayedexpansion ECHO 正 ...

  9. nginx 配一个简单的静态文件服务器 和一个虚似机

    下面是个图片服务器: server { listen ; server_name img.xxx.xxx.com; root /data/site/img.xxx.xxx.com; access_lo ...

  10. CLR via C#(08)-操作符

    对于操作符,我们并不陌生,例如+,-,*,%等二元操作符,以及++,!等一元操作符.但是对于非基元类型,我们需要通过一些自定义方法才能使用这些操作符.今天主要和大家分享关于操作符重载和转换操作符的知识 ...