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 ...
随机推荐
- ASP.net的身份验证方式有哪些?
[转] ASP.net的身份验证方式有哪些?分别是什么原理? Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验 ...
- [转载]JAVA调用Shell脚本
FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...
- Shell命令-----VI
vi的基本操作 a) 进入vi 在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi file 不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(comman ...
- 两种“新型”的javaweb后门(jspx和Java Logger)
利用这个可以突破st2下 强制jsp跳转login.jsp 利用jspx解决jsp后缀被限制拿shell - Hack Blog | 黑客博客http://www.hackblog.cn/post ...
- mui 本地打包
第一步:下载HBuilder离线打包Android版SDK 网址:http://ask.dcloud.net.cn/article/38 第二步:导入项目 启动带ADT的eclipse程序,菜单中选择 ...
- 【Mysql】 你会用 information_schema吗?
示例 select * from information_schema.views 其中的views可以替换成以下的字段,以下未列举的一般的数据库操作工具,在information_schema后输入 ...
- 用SwiftGen管理UIImage等的String-based接口
代码地址如下:http://www.demodashi.com/demo/12149.html 问题现状 平时我们使用UIImage,UIFont,UIColor会遇到很多String-based的接 ...
- JAVA学习第四十八课 — IO流(二):文件的复制 & 缓冲区1
一.复制文本文件 将G盘的文本文件拷贝到D盘上 也就是 读取G盘中文本文件的数据.写入D盘中->连读带写 而剪切呢.就是连读带写后,删除原盘的文件 <span style="fo ...
- 网络爬虫与搜索引擎优化(SEO)
一.网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引. ...
- Java除法结果带小数、进一法的实现 Java问题通用解决代码
http://blog.csdn.net/windone0109/article/details/5355379进一法: 即省略的位上只要大于零都要进一位 : 四舍五入法: 即省略的位上小于五都要舍 ...