最小重量机器设计问题

来源:互联网 发布:地球生态数据 编辑:程序博客网 时间:2024/06/08 03:40
#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 41 2 33 2 12 2 21 2 33 2 12 2 2*/

原创粉丝点击