码迷,mamicode.com
首页 > 其他好文 > 详细

背包问题扩展

时间:2015-03-13 20:16:45      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

背包问题学习链接:

http://blog.csdn.net/kangroger/article/details/38864689

代码:

#include <stdio.h>
#include <string.h>
int invest[301][21];
int total_invest;
int company_num;
int max[301];

int main(void)
{
    int tc, T;
    
    freopen("input.txt", "r", stdin);

    setbuf(stdout, NULL);

    scanf("%d", &T);
    for(tc = 0; tc < T; tc++)
    {
        total_invest = 0;
        company_num = 0;

        memset(max, 0, sizeof(max));

        scanf("%d %d",&total_invest,&company_num);
        
        int i,j,k;

        for(i=1;i<=total_invest;i++)
        {
            for(j=0;j<=company_num;j++)
            {
                scanf("%d",&invest[i][j]);
            }
        }
        
        for(j=1;j<=company_num;j++)
        {            
            for(i=total_invest;i>=1;i--)
            {
                int tmp = -1;

                for(k=1;k<=i;k++)
                {
                    if(invest[k][j] + max[i-k] > max[i])
                    {
                        max[i] = invest[k][j] + max[i-k];
                    }
                }
            }

        }
        
        printf("%d\n",max[total_invest]);        

    }

    return 0;//Your program should return 0 on normal termination.
}

 

测试数据:

5
4 2
1 3 3
2 4 4
3 7 6
4 8 8
7 2
1 3 3
2 4 4
3 7 6
4 8 8
5 10 10
6 13 13
7 15 15
5 3
1 4 4 3
2 6 7 6
3 8 8 9
4 13 13 13
5 15 14 15
10 3
1 4 4 3
2 6 7 6
3 8 8 9
4 13 13 13
5 15 14 15
6 19 18 19
7 20 20 20
8 23 25 23
9 27 27 26
10 31 31 31
5 5
1 3 4 6 2 6
2 11 10 10 9 11
3 12 12 13 14 13
4 18 17 19 19 18
5 23 26 24 25 24

测试结果:

10
16
17
33
28

 

背包问题扩展

标签:

原文地址:http://www.cnblogs.com/monkeyvera/p/4335731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!