您的位置:首页 > 其它

最小重量机器设计问题

2017-06-23 21:25 323 查看
#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

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: