老杨留的作业,折腾几个小时加上参考别人的终于写出来了,还是很弱啊。
效率貌似还可以,强化了分支定界的条件之后计算N=20的情况只需要十几秒。
好好体会一下~
#include #include #include #include using namespace std; #define N 15 int curLightestWeight = 1000000; int curDepth = 0; int curWeight = 0; int depth; int curCircle[N],bestCircle[N]; bool used[N]; int p[N][N]; void update(); void show(); void genGraphic(int maxWeight) { for(int i = 0;i < N;i++) { for(int j = 0;j < N;j++) { if(i == j) p[i][j] = 0; else p[i][j] = rand()%maxWeight + 1; } } for(int i = 0;i < N;i++) { for(int j = 0;j < N;j++) { cout<<<" "; } cout<= 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<<<"-->"; cout<<<<"The weight of the circle is "<<<"."< |