hdu 1114 Piggy-Bank(完全背包)

简介: 点击打开链接hdu1114 思路: 裸完全背包 代码: #include#include#include#includeusing namespace std;const int INF = 0x3f3f3f3f;cons...

点击打开链接hdu1114

思路: 裸完全背包

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 510;
const int MAXN = 10010;

int sum , n;
int p[N] , w[N];
int dp[MAXN];

void solve(){
    memset(dp , INF , sizeof(dp));
    dp[0] = 0;
    for(int i = 1 ; i <= n ; i++)
        for(int j = w[i] ; j <= sum ; j++)
           dp[j] = min(dp[j] , dp[j-w[i]]+p[i]); 
    if(dp[sum] != INF)
        printf("The minimum amount of money in the piggy-bank is %d.\n" , dp[sum]);
    else
        puts("This is impossible.");
}

int main(){
    int Case , x , y;
    scanf("%d" , &Case);
    while(Case--){
         scanf("%d%d" , &x , &y); 
         sum = y-x;
         scanf("%d" , &n);
         for(int i = 1 ; i <= n ; i++)
             scanf("%d%d" , &p[i] , &w[i]);
         solve(); 
    }
    return 0;
}



目录
打赏
0
0
0
0
15
分享
相关文章
hdu 2503 a/b + c/d
hdu 2503 a/b + c/d
56 0
HDU 2084 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
187 0
HDOJ/HDU 2568 前进(简单题)
HDOJ/HDU 2568 前进(简单题)
136 0
hdu 4771 Stealing Harry Potter's Precious
点击打开链接 题意:题目给定一个n*m的地图,地图有一个起点标记为'@',还有'#'表示不能够走的,'.'表示可以走。给定k个点,问从起点开始把这k个点走过去的最小步数。
804 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等