M - 基础DP
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
Input
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14 //第一行代表有T个测试案例
第二行 n,m 代表有 n 个物品,背包容量为 m 。接下来两行分别是 n 个物品的价值,体积 //动态规划入门,看了很久才懂。
我先用的递归做的 5696kb 452ms
#include <stdio.h>
#include <string.h> #define MAX 1005 int v[MAX];
int w[MAX];
int f[MAX][MAX]; int max(int a,int b)
{
return a>b?a:b;
} int dp(int n,int m)
{
if (f[n][m]>=) return f[n][m]; if (n==) return ; if (m<v[n])//fang bu liao
{
return dp(n-,m);
}
else
{
f[n][m]=f[n-][m];
f[n][m]=max(dp(n-,m),dp(n-,m-v[n])+w[n]);
}
return f[n][m];
} int main()
{
int i,t,n,m;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
scanf("%d",&w[i]);
for (i=;i<=n;i++)
scanf("%d",&v[i]); memset(f,-,sizeof(f)); printf("%d\n",dp(n,m));
}
return ;
}
递推 5592kb 78ms
#include <stdio.h> #define MAX 1005 int v[MAX];
int w[MAX];
int f[MAX][MAX]; int max(int a,int b)
{
return a>b?a:b;
} int main()
{
int i,j,t,n,m;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
scanf("%d",&w[i]);
for (i=;i<=n;i++)
scanf("%d",&v[i]); for (j=;j<=m;j++) f[][j]=; for (i=;i<=n;i++)
for (j=;j<=m;j++)
{
f[i][j]=f[i-][j];
if (j>=v[i])
f[i][j]=max(f[i-][j],f[i-][j-v[i]]+w[i]);
}
printf("%d\n",f[n][m]);
}
return ;
}
一维数组 1784kb 15ms
#include <stdio.h>
#include <string.h> #define MAX 1005 int v[MAX];
int w[MAX];
int f[MAX]; int max(int a,int b)
{
return a>b?a:b;
} int main()
{
int i,j,t,n,m;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
scanf("%d",&w[i]);
for (i=;i<=n;i++)
scanf("%d",&v[i]); memset(f,,sizeof(f));
for (i=;i<=n;i++)
for (j=m;j>=;j--)
{
if (j>=v[i])
f[j]=max(f[j],f[j-v[i]]+w[i]);
}
printf("%d\n",f[m]);
}
return ;
}
M - 基础DP的更多相关文章
- 基础dp
队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加 ...
- 基础DP(初级版)
本文主要内容为基础DP,内容来源为<算法导论>,总结不易,转载请注明出处. 后续会更新出kuanbin关于基础DP的题目...... 动态规划: 动态规划用于子问题重叠的情况,即不同的子问 ...
- hdu 5586 Sum 基础dp
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Desc ...
- hdu 4055 Number String (基础dp)
Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- 「kuangbin带你飞」专题十二 基础DP
layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...
- lightoj1004【基础DP】
从低端到顶端求个最大值: 思路: 基础DP,递推 #include<cstdio> #include<queue> #include<map> #include&l ...
- hdu 4489 The King’s Ups and Downs(基础dp)
The King’s Ups and Downs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
随机推荐
- django book多站点学习
多个站点 Django 的多站点系统是一种通用框架,它让你可以在同一个数据库和同一个Django项目下操作多个网站. 这是一个抽象概念,理解起来可能有点困难,因此我们从几个让它能派上用场的实际情景入手 ...
- SuperMap iClient如何使用WMTS地图服务(转)
http://blog.sina.com.cn/s/blog_6259ebd50102v221.html 什么是WMTS服务 WMTS,切片地图Web服务(Web Map Tile Service)当 ...
- window coordinate
到ndc的转换是通过(x/w,y/w,z/w) 到了 window coordinates dx upper left gl lower left setviewport setscissor rt ...
- js获取上传图片的尺寸大小
当上传图片时,有时候需要控制下上传图片的尺寸大小,需要给个提示 //获取图片的尺寸,控制尺寸大小 var reader = new FileReader(), img = new Image(); / ...
- ES6里关于类的拓展(二):继承与派生类
继承与派生类 在ES6之前,实现继承与自定义类型是一个不小的工作.严格意义上的继承需要多个步骤实现 function Rectangle(length, width) { this.length = ...
- python langid实现语种识别
2017-04-26 语料数据入库时有个小需求,需要用一个字段存储语料的语种,偶然发现langid可以实现这一功能,再次感叹python的好用! #coding=utf-8 import langid ...
- selenium的PageObject设计模式
PageObject设计模式1. Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 2. web测试时,建议 ...
- IDEA如何打包可运行jar的一个问题
转载:http://bglmmz.iteye.com/blog/2058785 背景: 有时候,我们会用IDEA来开发一些小工具,需要打成可运行的JAR包:或者某些项目不是WEB应用,纯粹是后台应用, ...
- Node.js 网页爬虫再进阶,cheerio助力
任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...
- linux下编译C/C++
对所有用户有效在/etc/profile增加以下内容.只对当前用户有效在Home目录下的.bashrc或.bash_profile里增加下面的内容:(注意:等号前面不要加空格,否则可能出现 comma ...