洛谷 P2918 [USACO08NOV]买干草Buying Hay

https://www.luogu.org/problem/P2918

JDOJ 2592: USACO 2008 Nov Silver 1.Buying Hay

https://neooj.com:8082/oldoj/problem.php?id=2592

题目描述

Farmer John is running out of supplies and needs to purchase H (1 <= H <= 50,000) pounds of hay for his cows.

He knows N (1 <= N <= 100) hay suppliers conveniently numbered 1..N. Supplier i sells packages that contain P_i (1 <= P_i <= 5,000) pounds of hay at a cost of C_i (1 <= C_i <= 5,000) dollars. Each supplier has an unlimited number of packages available, and the packages must be bought whole.

Help FJ by finding the minimum cost necessary to purchase at least H pounds of hay.

约翰的干草库存已经告罄,他打算为奶牛们采购H(1 \leq H \leq 50000)H(1≤H≤50000)镑干草.

他知道N(1 \leq N\leq 100)N(1≤N≤100)个干草公司,现在用11到NN给它们编号.第ii公司卖的干草包重量 为P_i (1 \leq P_i \leq 5,000)Pi​(1≤Pi​≤5,000) 磅,需要的开销为C_i (1 \leq C_i \leq 5,000)Ci​(1≤Ci​≤5,000) 美元.每个干草公司的货源都十分充足, 可以卖出无限多的干草包.

帮助约翰找到最小的开销来满足需要,即采购到至少HH镑干草.

输入格式

* Line 1: Two space-separated integers: N and H

* Lines 2..N+1: Line i+1 contains two space-separated integers: P_i and C_i

输出格式

* Line 1: A single integer representing the minimum cost FJ needs to pay to obtain at least H pounds of hay.

输入输出样例

输入 #1复制

2 15
3 2
5 3
输出 #1复制

9

说明/提示

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

裸的完全背包。

出了一些小bug。

我FSW以后要是再用memset置数组,我当场吃翔。

这道题一个坑点就是,你可以多买一些草,不一定非得是h,因为公司提供的是干草包,而干草包最多有5000,所以需要把范围多置5000,这是细节。

然后就是纯套完全背包模板。

祝大家刷题愉快。

#include<cstdio>
#include<algorithm>
using namespace std;
int n,h,ans=1e9;//n,种类数,h,总体积
int m[],w[];
int dp[];
int main()
{
for(int i=;i<=;i++)
dp[i]=1e9;
scanf("%d%d",&n,&h);
for(int i=;i<=n;i++)
scanf("%d%d",&m[i],&w[i]);
for(int i=;i<=n;i++)
for(int j=m[i];j<=h+;j++)
dp[j]=min(dp[j],dp[j-m[i]]+w[i]);
for(int i=h;i<=h+;i++)
ans=min(ans,dp[i]);
printf("%d",ans);
return ;
}

USACO Buying Hay的更多相关文章

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

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

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

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

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

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

  4. bzoj1618 / P2918 [USACO08NOV]买干草Buying Hay(完全背包)

    P2918 [USACO08NOV]买干草Buying Hay 显然的完全背包 设$f[i]$为买$i$磅干草的最小代价 搞搞完全背包即可 注意到最后可能买的干草超出范围,但是价格可能更低. 于是我们 ...

  5. 洛谷 P2918 [USACO08NOV]买干草Buying Hay 题解

    P2918 [USACO08NOV]买干草Buying Hay 题目描述 Farmer John is running out of supplies and needs to purchase H ...

  6. USACO Buying Feed, II

    洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II 洛谷传送门 JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II ...

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

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

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1618 裸的01背包,注意背包的容量不是v即可. #include <cstdio> #i ...

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

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

随机推荐

  1. mysql数据库的创建问题

    数据库客户端工具navicate 1.使用create database语句创建数据库 (1)指定字符集 create [database|schema ]if not exists 数据库名 def ...

  2. [LOJ 6288]猫咪[CF 700E]Cool Slogans

    [LOJ 6288]猫咪[CF 700E]Cool Slogans 题意 给定一个字符串 \(T\), 求一个最大的 \(K\) 使得存在 \(S_1,S_2,\dots,S_k\) 满足 \(S_1 ...

  3. js/java 获取、添加、修改、删除cookie(最全)

      一.cookie介绍 1.cookie的本来面目 HTTP协议本身是无状态的.什么是无状态呢,即服务器无法判断用户身份.Cookie实际上是一小段的文本信息(key-value格式).客户端向服务 ...

  4. ZooKeeper 入门看这篇就够了

    什么是 ZooKeeper? ZooKeeper 是一个分布式的,开放源码的分布式应用程序协同服务.ZooKeeper 的设计目标是将那些复杂且容易出错的分布式一致性服务封装起来,构成一个高效可靠的原 ...

  5. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  6. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  7. MySQL管理工具 -- MySQL Workbench

    管理MySQL,可以使用可视化图形界面MySQL Workbench.MySQL Workbench是一个图形客户端,可以用可视化的方式查询.创建和修改数据库表.它对MySQL的操作仍然是发送SQL语 ...

  8. 怎么做web接口测试

        这就需要开发提供的接口文档了,接口文档和功能测试的需求说明书的功能是一样的.包括:接口说明.调用的url,请求方式(get or post),请求参数.参数类型.请求参数说明,返回结果说明.有 ...

  9. 最简单取得外网IP

    #coding=utf-8import urllib2import re print re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http ...

  10. 纯C语言实现循环双向链表创建,插入和删除

    #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct DLNode{ Elem ...