Business investment

An investor intends to earn maximum profits by investing money in many companies. However, the investment unit is 100 million won. The more each company return profits the more the investor earns profits. On the other hand, if there is no investment, the investor has no profits. For example, an investor who has 400 million won invests 100 million won in two companies respectively, the earnable profits are the same as the below:

Invested Amount(Unit: KRW 100 million won)

Company A

Company B

1

5

1

2

6

5

3

7

9

4

8

15



If he invests 100 million won in Company A and 300 million won in Company B, an investor takes 1,400 million won (500 million won + 900 million won). The case to earn the maximum profit by investing 400 million won is investing 400 million won in Company B only, and at this time the profit is 1,500 million won. The investor cannot divide the money to one single company.

Upon the condition that the amount of investment and the number of companies are fixed, the profits are given to be earned by investing each company, create a program to find out investment method to earn the maximum profits and the profits.

Time limit: 1 second (java: 2 seconds)

Input format


Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 20) are given in a row.
Investment amount and the number of companies available for investment are given on the first row of each test case. In addition, investment amount and profits to be delivered to an investor by each company are given on each row from the second row. The total investing amount shall not be over 30 billion won, and the number of investable companies is 20 companies to the maximum.

Output format

Output the earnable maximum profit on the first row of each test case.

Example of Input

2
4 2
1 5 1
2 6 5
3 7 9
4 10 15
4 2
1 3 3
2 4 4
3 7 6
4 8 8

Example of Output

15

10                                          

/*
思路分析:
1,01背包问题,最早尝试把所有的点转换为 (公司数*投资数)个物品,进行简单的二重循环的背包问题,但是题意有说明,同一家
公司的投资不能分开,也就是这些物品之间有了排斥关系;
2,转换思路为,F[N][V]代表在前N家公司投资的总利益,应为投给了第N家和没有投给第N家两种情况的最大值;
F[N][V] = MAX{F[N-1][V], F[N-1][?]+?};
关键是这里投给第N家公司的最大利益需要一一遍历它的所有可能的投资数,然后得到max值,再代入上式进行比较即可。

		int max=-1;
		for(k=1;k<=amount;k++)
			if(j-k>=0)
				max = max>F[j-k]+inv_table[k][i]?max:F[j-k]+inv_table[k][i];
					
*/

#include <stdio.h>
#include <string.h>

int inv_table[301][21];
int amount, comp;
int F[301];

int main(void)
{
	int tc, T;
	
	setbuf(stdout, NULL);

	scanf("%d", &T);
	for(tc = 0; tc < T; tc++)
	{
		int i, j, k;
		scanf("%d %d", &amount, &comp);
		memset(F, 0, sizeof(F));
		
		
		for(i=1;i<=amount;i++)
			for(j=0;j<=comp;j++)
				scanf("%d", &inv_table[i][j]);

		for(i=1;i<=comp;i++)
			for(j=amount;j>=1;j--)
			{
				int max=-1;
				for(k=1;k<=amount;k++)
					if(j-k>=0)
						max = max>F[j-k]+inv_table[k][i]?max:F[j-k]+inv_table[k][i];
					
				F[j] = F[j]>max?F[j]:max;
			}
		
		printf("%d\n", F[amount]);
		
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值