AtCoder Beginner Contest 153 E.Crested Ibis vs Monster

AtCoder Beginner Contest 153 E.Crested Ibis vs Monster

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H . H. H.
Ibis can cast N N N kinds of spells. Casting the i i i-th spell decreases the monster’s health by A i A_i Ai, at the cost of B i B_i BiMagic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster’s health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

  • 1 ≤ H ≤ 1 0 4 1≤H≤10^4 1H104
  • 1 ≤ N ≤ 1 0 3 1≤N≤10^3 1N103
  • 1 ≤ A i ≤ 1 0 4 1≤A_i≤10^4 1Ai104
  • 1 ≤ B i ≤ 1 0 4 1≤B_i≤10^4 1Bi104
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H   N H N H N
A 1   B 1 A_1  B_1 A1 B1
:
A N   B N A_N  B_N AN BN

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815

这道题是典型的背包dp,就是完全背包条件下,找大于等于最大容量的最小价值

#include 
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const ll maxh = 5e4 + 10;
const ll maxn = 1010;
ll p[maxn], c[maxn];
ll dp[maxh];

int main()
{
    ll n, h;
    scanf("%lld%lld", &h, &n);
    for(ll i = 0; i < n; i++) scanf("%lld%lld", &p[i], &c[i]);
    fill(dp, dp +  maxh, INF);
    dp[0] = 0;
    for(ll i = 0; i < n; i++) {
        for(ll j = 0; j <= h; j++) {
            dp[j + p[i]] = min(dp[j + p[i]], dp[j] + c[i]);
        }
    }
    ll mins = INF;
    for (ll i = h; i < maxh; i++) mins = min(mins, dp[i]);
    printf("%lld\n", mins);
    return 0;
}

你可能感兴趣的:(动态规划,AtCoder)