M - 基础DP

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … 
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

The first line contain a integer T , the number of cases. 
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

One integer per line representing the maximum of the total value (this number will be less than 2 31).

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的更多相关文章

  1. 基础dp

    队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加 ...

  2. 基础DP(初级版)

    本文主要内容为基础DP,内容来源为<算法导论>,总结不易,转载请注明出处. 后续会更新出kuanbin关于基础DP的题目...... 动态规划: 动态规划用于子问题重叠的情况,即不同的子问 ...

  3. hdu 5586 Sum 基础dp

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Desc ...

  4. hdu 4055 Number String (基础dp)

    Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  6. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  7. 「kuangbin带你飞」专题十二 基础DP

    layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...

  8. lightoj1004【基础DP】

    从低端到顶端求个最大值: 思路: 基础DP,递推 #include<cstdio> #include<queue> #include<map> #include&l ...

  9. 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 ...

随机推荐

  1. ssh免密码登录之分发密钥

    ssh免密码登录之分发密钥 1.ssh免密码登录 密码登录和密钥登录有什么不同? 密码登录(口令登录),每次登录都需要发送密码(ssh) 密钥登录,分为公钥和私钥,公钥相当于锁,私钥相当于钥匙 1.1 ...

  2. Angular 学习笔记——$interpolateProvide

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  3. 关于Laravel5.2在php5.3.6X和在php7.1.10下的内存溢出

    php5.3.6X是编译安装,在debug模式下,频繁报出内存泄露警告 php7.1.10下则不会有此错误. 顺便提下:测试发现ThinkPHP也不会有该内存泄露警告! 希望知道如何解决该问题的童鞋能 ...

  4. TP框架中/ThinkPHP/Library/Think/Storage/Driver/File.class.php 错误

        /ThinkPHP/Library/Think/Storage/Driver/File.class.php LINE: 48错误 这是一个将windows下运行正常的ThinkPHP框架移到L ...

  5. DNS主从服务器

    一.目的: 我们知道,DNS服务器在网络服务中可能出现故障当机等状况,会导致DNS服务瘫痪,显然在实际的网络应用中我们不希望出现这种状况,所有我们就要配置从 服务器来在主DNS服务器出现故障时代替他来 ...

  6. Nginx实现虚拟主机

    因为IP地址有限,因此经常存在多个主机域名对应着同一个IP地址的情况,可以通过配置虚拟主机来解决这个问题. 在nginx.conf中,每个server块就是一个虚拟主机,它只会处理与其server_n ...

  7. 【Django】其他项目导入到Pycharm无法使用,报错:Error: Django is not importable in this environment

    导入项目后如下:项目名称那出现一个小叉 点击启动后提示错误: 那是由于运行环境的路径没有指向python的安装路径,如下图即可解决问题

  8. shell脚本通过ping命令来获取平均延时

    #!/bin/bash #设置环境变量 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin" exp ...

  9. Doker容器之间连接

    第一个应用容器 $ sudo docker run --name=mysql_client1 --link=mysql_server:db -t -i kongxx/mysql_client /usr ...

  10. ios 抓包工具 ios青花瓷charles

    iOS_青花瓷Charles抓包,ios青花瓷charles 使用青花瓷Charles抓取手机端的网络请求: 第一步,下载安装并打开Charles 第二步,去掉菜单[Proxy]以下的[Mac OSX ...