最小重量机器设计

 

C++语言: icesongqiang

 



#include < fstream>
#include < cstring>
#include < iostream>
#include < list>
#include < queue>
#include < vector>

using namespace std;

int d_n(0), d_m(0), d_d(0);
const int N = 10;
int d_c[N][N];
int d_w[N][N];

//const int MAX = 1 << 10;
//int d_min_weight(MAX);

typedef struct Node
{
   int i;
   int j;
   int c;
   int w;
   int total_weight;
   vector<int>n_select;

   Node(){
       i =0;
       j =0;
       c= 0;
       w = 0;
       total_weight = 0;
   }

   bool operator < (const Node & b) const {
       if (i < b.i)
           return true;
       else if (i == b.i && total_weight > b.total_weight) return true;
       
       
       else return false;
   }
}Node;

void ReadFile()
{
   std::ifstream infile("input.txt");
   infile >> d_n >> d_m >> d_d;
   cout << "d_c:\n";
   for (int i = 0; i != d_n; i++){
       for (int j = 0; j != d_n; j++){
           infile >> d_c[i][j];
           printf("%d ", d_c[i][j]);
       }
       printf("\n");
   }
   cout << "d_w:\n";
   for (int i = 0; i != d_n; i++){
       for (int j = 0; j != d_n; j++){
           infile >> d_w[i][j];
           printf("%d ", d_w[i][j]);
       }
       printf("\n");
   }
}


void WriteFile(Node & endnode)
{
   std::ofstream outfile("output.txt");
   outfile << endnode.total_weight << std::endl;
   
   for (vector<</span>int>::iterator it = endnode.n_select.begin(); it != endnode.n_select.end(); ++it)
       outfile << *it << "\t";
}

void search()
{
   //priority_queue ,NodeCmp> pq;
   priority_queue<</span>Node> pq;
   Node root;
   pq.push(root);

   while (!pq.empty()){
       Node cur = pq.top();
       cout << cur.i << " " << cur.j << " " << cur.c << " " << cur.w << " " << cur.total_weight<< "\n";
       if (cur.i == d_n){ cout << "find!\n"; WriteFile(cur); return; }  
       pq.pop();
       Node add;
       add.i = cur.i + 1;                            
       for (int j = 1; j <= d_m; ++j){              
           add.w = d_w[add.i - 1][j - 1];
           add.total_weight = cur.total_weight + add.w;
           if (add.total_weight > d_d) continue;
           add.j = j;
           add.c = d_c[add.i-1][j - 1];
           add.n_select.assign(cur.n_select.begin(),cur.n_select.end());
           add.n_select.push_back(j);
           pq.push(add);
       }
   }
}

int main()
{
   ReadFile();
   search();
   cout << "\nover";
   system("pause");
   return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值