列表

详情


NC24979. [USACO 2008 Nov S]Buying Hay

描述

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 Pi (1 <= Pi <= 5,000) pounds of hay at a cost of Ci (1 <= Ci <= 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.

输入描述

* Line 1: Two space-separated integers: N and H
* Lines 2..N+1: Line i+1 contains two space-separated integers: Pi and Ci

输出描述

* 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 

输出:

9

说明:

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 4ms, 内存消耗: 552K, 提交时间: 2022-01-10 19:17:32

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int dp[51000],v[51000],w[51000];

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i){
		scanf("%d%d",&v[i],&w[i]);
	}
	memset(dp,INF,sizeof(dp));
	dp[0]=0;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(j>=v[i]){
				dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
			}else{
				dp[j]=min(dp[j],w[i]);
			}
		}
	}
	printf("%d\n",dp[m]);
} 

Python3 解法, 执行用时: 608ms, 内存消耗: 6000K, 提交时间: 2022-10-03 20:46:59

a=[0 for i in range(50005)]
n,h=map(int,input().split())
ans=99999999
for j in range(n):
    q,t=map(int,input().split())
    s=1
    while 1:
        if s>=t:
            a[s]=max(a[s],a[s-t]+q)
        if a[s]>=h:
            ans=min(s,ans)
            break
        s+=1
print(ans)

上一题