最小重量机器设计问题
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 */
相关文章推荐
- 最小重量机器设计问题 5_1 6_4
- 最小重量机器设计问题 回溯法
- 最小重量机器设计问题(回溯)
- 回顾下最小重量机器设计问题
- 南邮 OJ 1278 最小重量机器设计问题
- 最小重量机器设计问题
- 最小重量机器设计问题
- 算法分析最小重量机器设计问题
- 算法 最小重量机器设计问题
- 最小重量机器设计问题
- 最小重量机器设计问题
- 最佳运动员配对问题-婚姻搭配-最小重量机器设计问题-回溯法
- 最小重量机器设计问题
- 回溯法算法思想解决最小机器重量设计问题
- #分支限界法#最小机器重量设计问题(优先队列)
- SDIBTOJ1350-最小重量机器设计问题
- 最小重量机器设计问题
- 最小重量机器设计问题
- 最小机器重量设计问题
- 最小重量机器设计问题