Investment-----POJ_2063-----完全背包问题

来源:互联网 发布:win10网络唤醒 编辑:程序博客网 时间:2024/06/08 15:38

题目地址:http://poj.org/problem?id=2063

Investment
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 4792 Accepted: 1670

Description

John never knew he had a grand-uncle, until he received the notary's letter. He learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor. 
John did not need that much money for the moment. But he realized that it would be a good idea to store this capital in a safe place, and have it grow until he decided to retire. The bank convinced him that a certain kind of bond was interesting for him. 
This kind of bond has a fixed value, and gives a fixed amount of yearly interest, payed to the owner at the end of each year. The bond has no fixed term. Bonds are available in different sizes. The larger ones usually give a better interest. Soon John realized that the optimal set of bonds to buy was not trivial to figure out. Moreover, after a few years his capital would have grown, and the schedule had to be re-evaluated. 
Assume the following bonds are available: 
ValueAnnual
interest4000
3000400
250

With a capital of e10 000 one could buy two bonds of $4 000, giving a yearly interest of $800. Buying two bonds of $3 000, and one of $4 000 is a better idea, as it gives a yearly interest of $900. After two years the capital has grown to $11 800, and it makes sense to sell a $3 000 one and buy a $4 000 one, so the annual interest grows to $1 050. This is where this story grows unlikely: the bank does not charge for buying and selling bonds. Next year the total sum is $12 850, which allows for three times $4 000, giving a yearly interest of $1 200. 
Here is your problem: given an amount to begin with, a number of years, and a set of bonds with their values and interests, find out how big the amount may grow in the given period, using the best schedule for buying and selling bonds.

Input

The first line contains a single positive integer N which is the number of test cases. The test cases follow. 
The first line of a test case contains two positive integers: the amount to start with (at most $1 000 000), and the number of years the capital may grow (at most 40). 
The following line contains a single number: the number d (1 <= d <= 10) of available bonds. 
The next d lines each contain the description of a bond. The description of a bond consists of two positive integers: the value of the bond, and the yearly interest for that bond. The value of a bond is always a multiple of $1 000. The interest of a bond is never more than 10% of its value.

Output

For each test case, output – on a separate line – the capital at the end of the period, after an optimal schedule of buying and selling.

Sample Input

110000 424000 4003000 250

Sample Output

14050

Source

Northwestern Europe 2004

此题的题目大意就是给你一笔钱,给你几年的时间,然后这里有一些股票,每个股票有一定的花费,但是会带来一定的利益,在给定的几年时间里每年同种股票能买多个,这就成了一个完全背包问题,即每种物品可以用无限次。然后我们要做的事情就是对每一年根据手上有的钞票通过完全背包算法求出最大收益,然后再把这些收益和以前的本金放在一起在下一年继续买股票。直到达到几年的时间结束为止。另外有一个小技巧就是因为股票的价格都是1000的倍数,所以可以将他们除以1000来减小时间和空间复杂度,是算法更加精简。废话少说,上代码:

#include<iostream>using namespace std;#define MAX 20#define MAX2 10001000int f[MAX2];  //f[j]  保存的是在这一年用j价值的money买股票所带来的最大收益 int price[MAX];int money[MAX];int mmax(int a,int b){if(a>b)return a;return b;}int main(){int t;scanf("%d",&t);while(t--){int allmoney,years;  //allmoney保存的是当前可用来买股票的前 int rallmoney;//rallmoney保存的是最大收益 scanf("%d%d",&allmoney,&years);rallmoney = allmoney;int n;scanf("%d",&n);int i,m;for(i=1;i<=n;i++){scanf("%d%d",&price[i],&money[i]);price[i]/=1000;  //以为每种股票的价格是1000的倍数,所以可以除以1000来优化 }while(years--){allmoney = allmoney/1000;for(i=0;i<=allmoney;i++)f[i] = 0;for(i=1;i<=n;i++)//用完全背包算法算出这一年的最大收益 {for(m = price[i];m<=allmoney;m++)f[m] = mmax(f[m],f[m-price[i]]+money[i]);}allmoney = rallmoney + f[allmoney];  //此时的f[allmoney]得到的就是这一年的最大收益 rallmoney = allmoney;}printf("%d\n",rallmoney);}return 0;}


原创粉丝点击