Luo Gu P2918 [USACO08NOV] buy hay Buying Hay solution to a problem

P2918 [USACO08NOV] buy hay Buying Hay

Title Description

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.

John hay stocks have been exhausted, he intends to purchase cows \ (H (1 \ leq H \ leq 50000) \) pounds of hay.

He knew \ (N (1 \ leq N \ leq 100) \) a hay companies now use \ (1 \) to \ (N \) to number them. The first \ (I \) company selling hay bales weight \ (P_i (1 \ leq P_i \ leq 5,000) \) pounds, the overhead required to \ (C_i (1 \ leq C_i \ leq 5,000) \) dollars each company's hay supply are very adequate, you can sell unlimited many bales of hay.

Help John find the minimum cost to meet the needs of that purchase at least \ (H \) pounds of hay.

Input Format

  • 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

Output Format

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

Sample input and output

Input # 1

2 15
3 2
5 3

Output # 1

9

Description / Tips

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

[Thinking]

Full backpack
very interesting topic a backpack

[Title] effect

Give you hay weight and cost, each of which is unlimited buy
buy less than h pound hay spend the least money

[Analysis] title

It is clear that you can do with a backpack full of
data range 5000 * 100 no problem
then it can be run in accordance with the template completely backpack,
bb [i] represents the least amount of money i spent £ hay
can be made ia [x] push over a case
(a [x] denotes any one of hay)
is bb [ia [x]] + c [x] in the case of push over a minimum
and found that only 30 minutes

【wrong reason】

At least pound H hay which is very confusing and
can not help but ignore him
but in fact this is the most important place
at least H that you can buy a pound more hay than H
but H pounds and more than have a limitation
because a bundle of hay Up to 5,000 pounds
so a maximum of H + 5000 pounds
Why? ?
Because if a exceeds 5000 pounds
it must have a bundle of hay out completely superfluous
i.e. after any bundle of hay can be removed or at least satisfy H £ hay
because of any bundle of hay is less than 5000 pounds
H plus a greater than or equal minus a less than equal to the number after number 5000 5000
or at least meet the H pounds of hay
so the extra range after 5000 and that is not necessary for the
so run full backpack when running H + 5000
and the final answer is the H-H within this range minimum +5000

[Complete code]

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int Max = 55004;
int bb[Max],p[105],c[105];

int main()
{
    int n,h;
    cin >> n >> h;
    memset(bb,999999,sizeof(bb));
    for(register int i = 1;i <= n;++ i)
        cin >> p[i] >> c[i];
    bb[0] = 0;
    for(register int i = 1;i <= n;++ i)
        for(register int j = p[i];j <= h + 5000;++ j)
            bb[j] = min(bb[j],bb[j - p[i]] + c[i]);
    int M = 0x7fffffff;
    for(register int i = h;i <= h + 5000;++ i)
        M = min(M,bb[i]);
    cout << M << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11711430.html