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 ...
随机推荐
- 排序算法的实现(冒泡,选择,插入 O(N*N)--理解方法实现
以前也看过很多排序算法的原理,每次都想自己实现一下,一直都再拖,现在着牛课网学习算法课程,希望自己能够坚持练习. //对于一个int数组,请编写一个选择冒泡算法,对数组元素排序. //给定一个int数 ...
- Linux alias理解及设置
1.alias简介 Linux alias 是命令的一种别称,输入 alias 可以看到像下面这样的结果: alias l.='ls -d .* --color=auto' alias ll='ls ...
- mysql 存储过程 演示样例代码
drop procedure if exists P_SEQUENCE; /** 暂省略包 @AUTO LIANGRUI 2014/6/27 T_PRO_PRODUCT 表 排序 对整个表进行按序号排 ...
- Oracle基础 索引
一.索引 索引是一种快速访问数据的途径,可提高数据库性能.索引使数据库程序无须对整个表进行扫描,就可以在其中找到所需的数据,就像书的目录,可以快速查找所需的信息,无须阅读整本书. (一)索引的分类 逻 ...
- React Native 项目实战 -- DoubanProject
引言:本文是我研究react-native时写的一个简单的demo,代码里有详细的注释,好废话不多说,直接上代码. 1.项目目录 2.index.android.js /** * index.andr ...
- PJSIP dialog inv销毁
PJSIP的Diaglog(类型为pjsip_dialog) 可以被外部调用,同时PJSIP有自己的机制销毁用户创建的 Dialog,如PJSIP内部销毁了某个Diaglog,用户在不知情的情况下继续 ...
- java调用本地默认浏览器
1 http://blog.csdn.net/casularm/article/details/3401018 2
- 转:HTTP ---HTTP头的编码问题(Content-Disposition)
最近在做项目时遇到了一个 case :需要实现一个强制在浏览器中的下载功能(即强制让浏览器弹出下载对话框),并且文件名必须保持和用户之前上传时相同(可能包含非 ASCII 字符). 前一个需求很容易实 ...
- LeetCode226 InvertBinaryTree Java题解
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解答: 遍历每个节点 直接交换他们的 ...
- [译]GLUT教程 - 渲染到子窗体
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Rendering to Subwindows 先回顾一下之前 ...