新人求大神纠错这个基于Hamilton回路算法的C语言程序
#include <cstdlib>#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define N 11
int curLightestWeight = 1000000;
int curDepth = 0;
int curWeight = 0;
int depth;
int curCircle[N],bestCircle[N];
bool used[N];
float p[N][N];
void update();
void show();
void genGraphic(int maxWeight)
{ int i;
for(i = 0;i < N;i++)
{
for(int j = 0;j < N;j++)
{
scanf("%f",&p[i][j]);
}
}
for(i = 0;i < N;i++)
{
for(int j = 0;j < N;j++)
{
cout<<p[i][j]<<" ";
}
cout<<endl;
}
}
void MHC_recursion(int curVertex)
{
curCircle[curDepth] = curVertex;
curDepth++;
used[curVertex] = true;
if(curWeight + (depth - curDepth) >= curLightestWeight)
{
curDepth--;
used[curVertex] = false;
return;
}
else if(curDepth == depth)
{
int thisWeight = p[curVertex][0];
curWeight += thisWeight;
if(curWeight < curLightestWeight)
{
curLightestWeight = curWeight;
update();
}
curDepth--;
used[curVertex] = false;
curWeight -= thisWeight;
return;
}
else
{
for(int i = 0;i < N;i++)
{
if(i == curVertex || used[i] == true)
continue;
int thisWeight = p[curVertex][i];
curWeight += thisWeight;
MHC_recursion(i);
curWeight -= thisWeight;
}
}
used[curVertex] = false;
curDepth--;
return;
}
void update()
{
for(int i = 0;i < N;i++)
bestCircle[i] = curCircle[i];
}
void show()
{
for(int i = 0;i < N;i++)
cout<<bestCircle[i]<<"-->";
cout<<bestCircle[0]<<endl;
cout<<"The weight of the circle is "<<curLightestWeight<<"."<<endl;
}
void init()
{
curDepth = 0;
curWeight = 0;
curLightestWeight = 1000000;
depth = N;
}
int main(int argc, char *argv[])
{
cout<<"******************************************\n";
cout<<"** 边不重复的Hamilton回路 **\n";
cout<<"**-------------------------**\n";
cout<<"** 程序提示: **\n";
cout<<"1.使用之前,请将图中节点从0开始连续编号.\n";
cout<<"2.节点个数为11.\n";
cout<<"3.节点个数上限可在源程序中修改.\n";
cout<<"**-------------------------**\n";
cout<<"请输入景点矩阵元素:\n";
genGraphic(9);
init();
MHC_recursion(0);
show();
system("PAUSE");
return EXIT_SUCCESS;
}