一个是递归算法
一个是STL算法
float Combin( CITY a[] , int k, int m)
{
int i;
if (k == m)
{//get an arrang
float temp=0;
for ( i=0; i<cityNum-1; i++)
{
temp += GetPointDistance(a[i], a[i+1]);
}
//保存最短距?
if (temp < all_distance || count == 1 )
{
all_distance = temp;
//将最短路径排列保存在 外部
for (i=0; i<cityNum; i++)
citys[i] = a[i];
}
count++;
}
else
{
for (i=k; i<=m; i++)
{
swap(a[k], a[i]);
Combin(a , k+1, m);
swap(a[k], a[i]);
}
}
return all_distance;
}
float STL_Arrange(CITY city[])
{
int i;
vector<int> a(cityNum-2,0);
for(i=0;i<cityNum-2;i++)
{
a[i] = i+1;
}
float temp=0;
for ( i=0; i<cityNum-1; i++)
{
temp += GetPointDistance(city[i], city[i+1]);
}
all_distance = temp;
count++;
while ( std::next_permutation(a.begin(), a.end()) )
{
for(i=1; i<cityNum-1; i++)
{
swap(city[i],city[a[i-1]]);
}
temp = 0;
for ( i=0; i<cityNum-1; i++)
{
temp += GetPointDistance(city[i], city[i+1]);
}
//保存最短距?
if (temp < all_distance )
{
all_distance = temp;
//将最短路径排列保存在 外部
for (i=0; i<cityNum; i++)
citys[i] = city[i];
}
count++;
}
return all_distance;
}