hdu 2602 dp 01背包

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602

这题是非常标准的01背包,没啥特殊的地方,很简单

代码:

#include <bits/stdc++.h>
#define MAXS   1006
using namespace std;

int value[MAXS];

int main ()
{
  int T;
  int n,m_v,v;
  int f[MAXS] ;
  cin >> T;
  while(T--)
  {
      memset(f,0,sizeof(f));
    scanf ("%d%d",&n,&m_v);
    for (int i=0;i<n;++i)
     scanf("%d",&value[i]);
    for (int i = 0;i<n;++i)
      {
          scanf("%d",&v);
          for (int j = m_v; j>= v;--j)
            f[j] = max (f[j],f[j-v] + value[i]);
      }
     printf("%d\n", f[m_v]);
  }

 return 0;
}

原文地址:https://www.cnblogs.com/yuluoluo/p/8900805.html

时间: 2024-10-10 17:45:31

hdu 2602 dp 01背包的相关文章

hdu 1171 Big Event in HDU(dp 01背包 母函数)

01背包 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; int v[100],m[100]; int dp[300000]; int main() { int n; int i,j,k; while(scanf("%d",&n)!=EOF) {

hdu 2602(01背包)

01背包:有N件物品和一个容量为V 的背包.放入第i件物品耗费的空间是Ci,得到 的价值是Wi.求解将哪些物品装入背包可使价值总和最大.(每件物品只有一件,放或者不放) 即F[i,v]表示前i件物品恰放入一个容量为v的背包可以 获得的最大价值.则其状态转移方程便是:F[i,v] = max{F[i−1,v],F[i−1,v−Ci] + Wi} 我们可以做一点空间上的优化,用F[v]表示当前v空间所获得的最大价值,则F[v]=max(F[v],F[v-Ci]+W[i]); 这里要注意一个问题,空间

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

HDU 3033 I love sneakers! (DP 01背包+完全背包)

Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. There are several brands of sneakers that Iserlohn wan

HDU 2955 Robberies --01背包变形

这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即转化为01背包问题. 此时dp[v]表示抢劫到v块钱成功逃跑的概率,概率相乘. 最后从大到小枚举v,找出概率大于逃跑概率的最大v值,即为最大抢劫的金额. 代码: #include <iostream> #include <cstdio> #include <cstring>

Jam&#39;s balance HDU - 5616 (01背包基础题)

Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side are the same weight. Weights can be put on left side or right side arbitrarily. Please tell whether the balance can measure an object of weight M. In

USACO Money Systems Dp 01背包

一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V; ++j){ if(i - a[j] > 0){ dp[i] += dp[i - a[j]]; } } } 状态存在冗余, 输出的时候答案肯定不对 但只需要改一下两个for循环的顺序即可. Source Code: /* ID: wushuai2 PROG: money LANG: C++ */ //

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (Interna