最小重量机器设计问题

#include<iostream>
#include<cstdio>
#include<fstream>
#include<cstring>
#include<queue>
using namespace std;
int n,m,d;
int price[100][100];
int weight[100][100];
int minw=99999;
string answer;
struct Node
{
    int geshu;
    int sum_cost;
    int sum_weight;
    string note;
} node,temp;
void dfs()
{
    queue<Node>que;
    for(int j=1; j<=m; ++j)
    {
        node.geshu=1;
        node.sum_cost=price[1][j];
        node.sum_weight=weight[1][j];
        node.note+=(char)(j+'0');
        if(node.sum_cost<=d)
            que.push(node);
    }
    while(!que.empty())
    {
        temp=que.front();
        que.pop();
        //cout<<temp.geshu<<" "<<temp.sum_cost<<" "<<temp.sum_weight<<endl;测试用
        int i=temp.geshu;
        if(i==n)
        {
            if(temp.sum_cost<minw)
            {
                minw=temp.sum_cost;
                answer=temp.note;
            }
        }
        else
        {
            for(int j=1; j<=m; ++j)
            {
                int c=temp.sum_cost;
                int w=temp.sum_weight;
                if((c+price[i+1][j]<=d)&&(w+weight[i+1][j]<minw))
                {
                    Node temp1;
                    temp1.geshu=i+1;
                    temp1.sum_cost=price[i+1][j]+temp.sum_cost;
                    temp1.sum_weight=weight[i+1][j]+temp.sum_weight;
                    temp1.note=temp.note+" "+char(j+'0');
                    que.push(temp1);
                }
            }
        }
    }
}
int main()
{
    cin>>n>>m>>d;
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            //第i种零件在第j个商店中的价格
            cin>>price[i][j];
        }
    }
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=m; ++j)
        {
            //第i种零件在第j个商店中的重量
            cin>>weight[i][j];
        }
    }
    dfs();
    cout<<"总重量为"<<minw<<endl;
    cout<<"商品分别是在以下几个商店购买"<<endl;
    cout<<answer;
    cout<<endl;
}
/*
3 3 4
1 2 3
3 2 1
2 2 2
1 2 3
3 2 1
2 2 2

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值