开一个数组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>
#include <string>
#include<iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include <queue>
#include <vector>
#define IN freopen("in.txt","r",stdin);
int a[30],dp[30][MAXN],p[MAXN];
void out(int x)
{
if(x==0)
return ;
else out(x-p[x]);
printf("%d ",p[x]);
}
int main()
{
int n,m;
// IN;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(p,0,sizeof(p));
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1; i<=n; i++)
{
for(int j=m; j>0; j--)
{
if(j>=a[i]&&dp[i-1][j]<dp[i-1][j-a[i]]+a[i])
dp[i][j]=dp[i-1][j-a[i]]+a[i],p[j]=a[i];
else
dp[i][j]=dp[i-1][j];
}
}
int i;
for(i=m;i>=0;i--)
if(dp[n][i]>dp[n][i-1])
break;
out(i);
printf("sum:%d\n",dp[n][m]);
}
return 0;
}

【DP】UVA 624 CD 记录路径的更多相关文章

  1. hdu1074 状压DP、栈实现记录路径

    题意:给了几门学科作业.它们的截止提交期限(天数).它们的需要完成的时间(天数),每项作业在截止日期后每拖延一天扣一学分,算最少扣的学分和其完成顺序. 一开始做的时候,只是听说过状态压缩这个神奇的东西 ...

  2. UVA 624 CD(01背包+输出方案)

    01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...

  3. UVA 624 CD(01背包,要记录路径)

    题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...

  4. UVA 624 CD【01背包+路径记录】

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

  5. UVA 624 CD[【01背包】(输出路径)

    <题目链接> 题目大意: 你要录制时间为N的带子,给你一张CD的不同时长的轨道,求总和不大于N的录制顺序 解题分析: 01背包问题,需要注意的是如何将路径输出. 由于dp时是会不断的将前面 ...

  6. UVA 624 CD(DP + 01背包)

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

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

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

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

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

  9. uva 624 CD (01背包)

      CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...

随机推荐

  1. js向标签中添加文本或其他的简例

    1.如何用js 在div内插入内容? 不是改变内容,而是插入,就是在保留原内容的基础上,在尾部添加.举个例子. 元内容<div>你好</div> 插入后<div>你 ...

  2. JSTL c:url

    c:url 标签 jstl 实例代码和用法.     <c:url>标记格式化成一个字符串格式的URL,并将其存储到变量中.这个标签会在必要时自动执行URL重写. var属性指定的变量将包 ...

  3. 【 Tomcat 】后端tomcat获取真实IP

    环境: nginx + tomcat nginx.conf 配置: proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_ad ...

  4. sql参数化防止sql注入导致的暴露数据库问题

    #转载请联系 假如你在京东工作,你要做的任务就是做一个商品搜索的东西供用户使用. 然后你写出了这么一个程序的雏形. import pymysql def main(): conn = pymysql. ...

  5. 好的web前端是如何拿到30万年薪的?

    2018年前端开发不再像过去几年里新技术框架层出不穷,而是各种组件,模块,很多东西都有痕迹可寻,技术都在原来的基础上有了革新和沉淀. 前端招聘方面也在紧跟技术发展,大量“滥竽充数”的速成开发者开始失去 ...

  6. django实现动态菜单的方式

    1.model from django.contrib.auth.models import User #django自带 class UserProfile(models.Model): " ...

  7. [centos6.5] 完全卸载httpd mysql php

    rpm -qa|grep mysql # 列出所有mysql相关包 rpm -e 包名 # 逐一卸载,一个方便技巧是:卸载时可以不带版本,比如 # mysqlclient10-3.23.58-4.RH ...

  8. luogu P1938找工就业

    一头牛在一个城市最多只能赚D元,然后它必须到另一个城市工作.当然它可以在别处工作一阵子后,又回到原来的城市再最多赚D美元.而且这样的往返次数没有限制城市间有P条单向路径,共有C座城市,编号1~C,奶牛 ...

  9. Gitlab运维

    安装Gitlab 新建 /etc/yum.repos.d/gitlab-ce.repo [gitlab-ce] name=gitlab-ce baseurl=http://mirrors.tuna.t ...

  10. windows下虚拟环境安装方法

    方法一:使用virtualenv安装虚拟环境 1.安装virtualenv pip install virtualenv 2.随意创建文件夹(将来虚拟环境会在这个文件夹)    mkdir mypro ...