[置顶] 九度笔记之 455:珍惜现在,感恩生活

题目1455:珍惜现在,感恩生活

 

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:133

解决:60

题目描述:

为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?

输入:

输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。

输出:

对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。

样例输入:
1
8 2
2 100 4
4 100 2
样例输出:
400

 

算法分析

       在本题中,不同种类的大米含有固定数量的袋数,我们可以把同一种类不同袋的大米单独看作一个物品,比如A类大米有4袋,我们就可以看作4个不同种类的大米,只不过他们的价格,重量是一样的。然后按照背包问题,用动态规划做就行了 。参考题目1209:最小邮票数

 

        std::cin>>price>>weight>>num;
        ricePrice.insert(ricePrice.end(),num,price);
        riceWeight.insert(riceWeight.end(),num,weight);

 

 普通背包问题
题目1364:v字仇杀队
题目1462:两船载物问题
题目1455:珍惜现在,感恩生活
题目1209:最小邮票数
题目1420:Jobdu MM分水果

项目安排类题目
题目1499:项目安排
题目1463:招聘会
题目1434:今年暑假不AC

资源无限求最大值的题目
题目1494:Dota


源程序

 

//============================================================================
// Name        : judo1455.cpp
// Author      : wdy
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
//similar to 1462
//similar t0 1455
//similar to 1364
#include <iostream>
#include <vector>
using namespace std;
void getMax(){
    int money;
    int riceClass;
    std::cin>>money>>riceClass;
 
    std::vector<int> ricePrice;
    std::vector<int> riceWeight;
    int price;
    int weight;
    int num;
    for(int rid = 0;rid<riceClass;rid++){
        std::cin>>price>>weight>>num;
        ricePrice.insert(ricePrice.end(),num,price);
        riceWeight.insert(riceWeight.end(),num,weight);
        /*
        for(int n = 0;n<num;n++){
            ricePrice.push_back(price);
            riceWeight.push_back(weight);
        }
        */
    }
 
    int *dp = new int[money+1];
    for(int i = 0;i<money+1;i++)
        dp[i] = 0;
 
    std::vector<int>::size_type riceNum = ricePrice.size();
    for(std::vector<int>::size_type rn = 0;rn<riceNum;rn++){
        for(int m = money; m>=ricePrice.at(rn); m--)
            dp[m] = max(dp[m], dp[m-ricePrice.at(rn)]+ riceWeight.at(rn));
    }
    std::cout<<dp[money]<<std::endl;
 
}
void judo(){
    int c = 0;
    while(std::cin>>c){
        for(int i = 0;i<c;i++)
        getMax();
    }
}
int main() {
    int c = 0;
    judo();
 
    return 0;
}
 
/**************************************************************
    Problem: 1455
    User: KES
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/


 

 

posted @ 2013-08-07 17:53  javawebsoa  Views(225)  Comments(0Edit  收藏  举报