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;
}