C++ 旅行商的多片段启发式算法(C+;+;)
我正在尝试实现TSP的多片段启发式算法 算法是这样的:C++ 旅行商的多片段启发式算法(C+;+;),c++,greedy,traveling-salesman,C++,Greedy,Traveling Salesman,我正在尝试实现TSP的多片段启发式算法 算法是这样的: 按权重的递增顺序对边进行排序。(可以任意断开连接。)将要构造的巡更边集初始化为空集 重复此步骤n次,其中n是正在求解的实例中的城市数:将排序边列表上的下一条边添加到巡更边集合,前提是此添加不会创建3度的顶点或长度小于n的循环;否则,跳过边缘 返回巡更边集 我一直在检查是否有循环 #include <stdio.h> #include <math.h> #include <limits.h> #includ
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <queue>
static int repeat[6];
struct element{
int distance;
int x;
int y;
};
struct element array[25];
void quicksort(struct element x[78400],int first,int last){
int pivot,j,i;
struct element temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i].distance<=x[pivot].distance&&i<last)
i++;
while(x[j].distance>x[pivot].distance)
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
bool isCycle(){
return true;
}
bool canAdd(int a){
repeat[array[a].x]++;
repeat[array[a].y]++;
if(repeat[array[a].x] > 2 || repeat[array[a].y] > 2){
repeat[array[a].x]--;
repeat[array[a].y]--;
return false;
}
return true;
}
int main(int argc, const char * argv[]) {
int i = 0;
int graph[6][6] = {
{INT_MAX,4,8,9,12},
{4,INT_MAX,6,8,9},
{8,6,INT_MAX,10,11},
{9,8,10,INT_MAX,7},
{12,9,11,7,INT_MAX}
};
int an = 0;
for(int a = 0; a<5; a++){
for(int b = a; b<5; b++){
array[an].x = a;
array[an].y = b;
array[an].distance = graph[a][b];
an++;
}
}
quicksort(array, 0, an-1);
static int sayilar[6];
for(int ya = 0; ya<6; ya++){
sayilar[ya] = 0;
}
std::queue<element> tour;
int edges = 0;
while(edges != 5){
printf("%d %d %d\n", array[i].x, array[i].y, array[i].distance);
if(canAdd(i)){
tour.push(array[i]);
printf("oldu\n");
if(isCycle()){
tour.pop();
}
}
i++;
edges = (int)tour.size();
printf("%d\n", edges);
}
return 0;
}
#包括
#包括
#包括
#包括
静态int重复[6];
结构元素{
整数距离;
int x;
int-y;
};
结构元素数组[25];
void快速排序(结构元素x[78400],int first,int last){
int轴,j,i;
结构元素温度;
如果(首先构建片段时,在每次迭代时检查更新片段的起始城市是否与最后一个相同
您可能需要更改您的实现以使此验证更容易
此外,我还邀请您阅读我的论文“旅行商问题的多片段巡回构造算法的实证研究”,该论文发表在《第16届混合智能系统国际会议论文集》(HIS 2016)上pp 278-287.DOI:10.1007/978-3-319-52941-7_28.要测试一个循环图,将两个指针设置到第一个边/顶点。每次迭代移动一个步骤,每次迭代移动另外两个步骤。在每次增量后检查两个指针是否相等。如果相等,则第二个指针已循环并捕捉,并且存在一个循环