【机器分配问题】

问题:

现有设备n台,可投放到m个项目中,每个项目的产量与投入该项目的设备数量有关。如表2所示为三个项目的产量(吨)和投入设备(台)的关系。求对m个项目的最优设备分配,使总产量效益最大。

表2 项目产出与投入的设备数量之间的关系

项目产出

投入设备数量     

项目1(吨)

项目2(吨)

项目3(吨)

1台

10 

25

11

2台

36

29

30

3台

40

43

45

4台

59

55

58

输入描述:

    输入第一行包含两个整数n,m,用空格隔开,分别表示设备的数量和项目的数量。其后有n行,每行有m个整数,分别表示1,…,n台设备分别投入到m个项目之后的产量产出。每个整数之间用空格隔开。

输出描述:

输出一个整数,表示将n台设备全部投入到m个项目中所能得到的最大产量。

样例输入:

       4 3

       10 25 11

       36 29 30

       40 43 45

       59 55 58

        输出:

        72

思路:

回溯法

代码:

#include<bits/stdc++.h>
using namespace std;
 
int n, m;
 
int dfs(int** relation, int* has, int cnt, int total)
{
    int result = 0;
 
    if(cnt > n) return total;
 
    for(int i = 1; i <= m; i++)
    {
        has[i] = has[i] + 1;
        int new_total = total+relation[has[i]][i]-relation[has[i]-1][i];
        int this_time = dfs(relation, has, cnt+1, new_total);
        has[i] = has[i] - 1;
        if(this_time > result) result = this_time;
    }
 
    return result;
}
void Delete(int** relation, int* has)
{
    for(int i = 0; i <= n+1; i++)
    {
        delete [] relation[i];
    }
 
    delete [] relation;
    delete [] has;
}
int main()
{
    
    cin >> n >> m;
 
    int **relation = new int*[n+2];
    int *has = new int[n+2]();
    for(int i = 0; i <= n+1; i++)
    {
        relation[i] = new int[m+2]();
    }
 
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cin >> relation[i][j];
        }
    }
 
    int result = 0;
    result = dfs(relation, has, 1, 0);
    cout << result << endl;
 
    Delete(relation, has);
 
    return 0;
}

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值