首页 > 其他分享 >[USACO08NOV]Buying Hay S

[USACO08NOV]Buying Hay S

时间:2023-06-02 23:14:41浏览次数:44  
标签:leq int 样例 Hay Line USACO08NOV 干草 Buying

[USACO08NOV]Buying Hay S

题目描述

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)\) 磅干草。

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

帮助约翰找到最小的开销来满足需要,即采购到至少 \(H\) 磅干草。

输入格式

* 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

样例输入 #1

2 15 
3 2 
5 3

样例输出 #1

9

提示

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

解析

这是一道完完全全的完全背包,但还是有些不一样,这里不是单一的装入,还要考虑什么什么情况下有特殊情况

当重量大于等于h,小于等于h与价格最大值的和时,最小值都有可能出现,不仅存于重量等于h时

状态转移方程如下:

\[f[j]=min(f[j],f[j-weight[i]]+val[i]); \]

即少载i头奶牛再加上载i头奶牛的时间

AC代码

#include <bits/stdc++.h>
using namespace std;
int n,m,ans=1e9;
int a[100],b[100],dp[55005];
int main()
{
	cin >> n >> m;
	for(int i=1;i<=m+5000;i++)
	{
		dp[i]=1e9;//初始化,因为要找到一个最小值,dp[i]表示得到i磅的干草最少需要的钱数 
	}
	for(int i=1;i<=n;i++)
	{
		cin >> a[i] >> b[i];
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=a[i];j<=m+5000;j++)
		{//注意循环结束为m+5000,因为你只购买m千克时花费的钱不一定是最少的,5000时一坨草质量的最大值 
			//完全背包 
			dp[j]=min(dp[j],dp[j-a[i]]+b[i]);
		}
	}

	for(int i=m;i<=m+5000;i++)
	{
		ans=min(ans,dp[i]);//寻找哪一个既符合购买量,钱又最少 
	}
		
	cout << ans;//直接输出即可 
	return 0;
}

标签:leq,int,样例,Hay,Line,USACO08NOV,干草,Buying
From: https://www.cnblogs.com/momotrace/p/p2918.html

相关文章

  • 洛谷 P4544 [USACO10NOV]Buying Feed G - 题解
    https://www.luogu.com.cn/problem/P4544感觉是很没意思的DP+DS优化,以前做过几道更难的题:https://codeforces.com/contest/1788/problem/Ehttps://atcoder.jp/contests/abc288/tasks/abc288_f这种题只要是让我写总是能把代码整的伤痕累累(逃第一眼:我艹不就是一个sbDP吗......
  • HayStack
    HayStack一、什么是Haystack?Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高),该框架支持Solr,Elasticsearch,Whoosh,**Xapian搜索引擎它是一个可插拔的后端(很像Django的数据库层),所以几乎你所有写的代码都可以在不同搜索引擎之间......
  • P4544 Buying Feed G
    dp&单调队列优化 这个题:k<=i,决策点k可以等于i,所以在i入队后递推#include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;constintN=503,M=1e4+2;#defineintlonglongintn,m,E,X[N],F[N],C[N],f[N][M];intq[N],hh,tt;......
  • Codeforces Round 644 (Div. 3) D. Buying Shovels(数论)
    https://codeforces.com/contest/1360/problem/DD.BuyingShovels题目大意:一个人想买正好n把铲子。店内有k种包装的铲子:第i种包装正好由i把铲子组成(1≤i≤k)。这家......
  • D. Buying gifts
    D.BuyinggiftsLittleSashahastwofriends,whomhewantstopleasewithgiftsontheEighthofMarch.Todothis,hewenttothelargestshoppingcenterin......
  • Pros and Cons of buying Premium Tech Tool
    Ifyou’veworkedonaVolvoorMackbefore,chancesareyou’veusedPremiumTechToolatsomepointinthepast.Iftheprogramactuallyworks,it’sagreat......
  • Codeforces 1360 D. Buying Shovels
    题意:要买个铲子,商店中有中不同的卖法,依次每一次卖到个铲子,现在只能选择其中的一种买法,问最少买几次同一种的买法,使得刚好买到直接选择小于的AC代码:intn,m,k;......
  • CF103E Buying Sets
    这个世界上怎么有这么巧妙的建模啊。。首先,题目保证了任意\(k\)个子集并的大小\(\gek\)。这说明我们选的数字的数量永远大于等于集合数量如果不考虑数字数量等于集......
  • déce. 23 Out of Hay S
    https://www.luogu.com.cn/problem/P1547最小生成树中的最长边是最后被加入的#include<bits/stdc++.h>usingnamespacestd;#defineinRead()typedeflonglongll;......
  • django-haystack使用whoosh创建索引
    快速入门环境安装首先需要清楚以下各个库的作用django是基于python开发的web框架,阅读本文需要了解相关的基础知识django-haystack为Django提供模块化搜索。它具有......