http://www.lydsy.com/JudgeOnline/problem.php?id=1618

裸的01背包,注意背包的容量不是v即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=105, oo=~0u>>1;
int f[50005+5005], v[N], w[N], n, V; int main() {
read(n); read(V);
int mx=0;
for1(i, 1, n) { read(v[i]); read(w[i]); mx=max(v[i], mx); }
for1(i, 1, V+mx) f[i]=oo>>1;
for1(i, 1, n) {
for(int j=v[i]; j<=mx+V; ++j)
f[j]=min(f[j], f[j-v[i]]+w[i]);
}
int ans=oo;
for(int j=V; j<=mx+V; ++j) ans=min(ans, f[j]);
print(ans);
return 0;
}

Description

    约翰的干草库存已经告罄,他打算为奶牛们采购日(1≤日≤50000)磅干草.
    他知道N(1≤N≤100)个干草公司,现在用1到N给它们编号. 第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅,需要的开销为Ci(l≤Ci≤5000)美元.每个干草公司的货源都十分充足,可以卖出无限多 的干草包.    帮助约翰找到最小的开销来满足需要,即采购到至少H磅干草.

Input

    第1行输入N和日,之后N行每行输入一个Pi和Ci.

Output

 
    最小的开销.

Sample Input

2 15
3 2
5 3

Sample Output

9

FJ can buy three packages from the second supplier for a total cost of 9.

HINT

Source

【BZOJ】1618: [Usaco2008 Nov]Buying Hay 购买干草(dp)的更多相关文章

  1. BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草( dp )

    无限背包dp.. 因为题目中说至少到 H 磅 , 我就直接把 H * 2 了.. ----------------------------------------------------------- ...

  2. BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草

    题目 1618: [Usaco2008 Nov]Buying Hay 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 679  Solved:  ...

  3. bzoj 1618: [Usaco2008 Nov]Buying Hay 购买干草【背包】

    好像是完全背包吧分不清了-- 好像是把数组二维压一维的时候,01背包倒序,完全背包正序 ```cpp include include using namespace std; const int N= ...

  4. [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草

    [BZOJ1618][Usaco2008 Nov]Buying Hay 购买干草 试题描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草. 他知道N(1≤N≤100)个干草 ...

  5. [Usaco2008 Nov]Buying Hay 购买干草[背包]

    Description     约翰的干草库存已经告罄,他打算为奶牛们采购日(1≤日≤50000)磅干草.     他知道N(1≤N≤100)个干草公司,现在用1到N给它们编号.第i个公司卖的干草包重 ...

  6. [Usaco2008 Nov]Buying Hay 购买干草

    题目描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草,他知道N(1≤N≤100)个干草公司,现在用1到N给它们编号.第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅 ...

  7. BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛( dp )

    状压dp dp( x , S ) 表示最后一个是 x , 当前选的奶牛集合为 S , 则状态转移方程 : dp( x , S ) =  Σ dp( i , S - { i } )  ( i ∈ S , ...

  8. BZOJ 1229: [USACO2008 Nov]toy 玩具

    BZOJ 1229: [USACO2008 Nov]toy 玩具 标签(空格分隔): OI-BZOJ OI-三分 OI-双端队列 OI-贪心 Time Limit: 10 Sec Memory Lim ...

  9. BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草( dp )

    -------------------------------------------------------------------- #include<cstdio> #include ...

随机推荐

  1. 算法笔记_027:俄式乘法(Java)

    1 问题描述 首先,了解一下何为俄式乘法?此处,借用<算法设计与分析基础>第三版上一段文字介绍: 2 解决方案 具体编码如下: package com.liuzhen.chapter4; ...

  2. 解析theme()

    drupal_render()只是对theme()的调用做了包装,真正做任务的还是theme(). function theme($hook, $variables = array()) { ... ...

  3. 用vue开发顶端粘滞效果的页面

    概述 通常一个长的页面,需要滚动浏览,有部分重要信息会随着滚动而看不见,因此需要粘滞在顶端,而又不影响滚动浏览,这个demo基于vue2,实现这个需求. 详细 代码下载:http://www.demo ...

  4. ref与out区别(ref有进有出,而out只出不进)

    ref与out区别(ref有进有出,而out只出不进)   C#基础:ref和out的区别 ref和out的区别在C# 中,既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值 ...

  5. 线程间操作无效: 从不是创建控件“txtreceive”的线程访问它。

    自己在写串口通信的时候遇到个这样的问题 自己是用vs2010  c#写的错误提示是这样的“线程间操作无效: 从不是创建控件“txtreceive”的线程访问它.”  用的控件是自带的serialPor ...

  6. Locust分布式负载测试工具入门

    忽略元数据末尾 回到原数据开始处 Locust简介 Locust是一个简单易用的分布式负载测试工具,主要用来对网站进行负载压力测试. 以下是github上的仓库地址 https://github.co ...

  7. 常用音频软件:Cool edit pro

    作者:桂. 时间:2017-06-02  11:51:08 链接:http://www.cnblogs.com/xingshansi/p/6932671.html 这里只涉及Cool edit pro ...

  8. MySQL Subquery

    Summary: in this tutorial, we will show you how to use the MySQL subquery to write complex queries a ...

  9. java开发常用到的jar包总结

    commons-io.jar: FileUtils 读取文件所有行 File file = new File("c:\\123.txt"); List<String> ...

  10. ui-router详解(二)ngRoute工具区别

    我们了解 angular.js 是一种富客户端单页面应用,所以要在一个页面呈现不同的视图,路由起到了至关重要的作用. angular.js 为我们封装好了一个路由工具 ngRoute ,它是一种靠ur ...