这个题目我用的模拟退火实现的,我们来观察这道题,这道题目显然是用贪心不行的,它没有多项式级别的解答,复杂度是:
模拟退火的核心是允许否定掉当前的最优解而选出并不是最优解的答案并且继续迭代,施瓦辛格说过要想攀登到另一座山,必须要从当前的山上下来,(放弃当前最优解,从而寻找可能的更优解)当然了,也有可能当前的山(山其实就是局部最优解)也有可能是全局最优解。
看一看代码的详细细节:
#define SHOW_CONSOLE
#include<bits/stdc++.h>
#include<graphics.h>
#define MAX_CITY_NUM 34
#define T0 2000
#define T 1e-5//温度最低截至到这个温度
#define ALPHA 0.98//每次的迭代参数
#define L 20000//初始温度
#define Pi 3.141592653589793238462643383
#define R 6371.004
using namespace std ;
int citys[50];
int ge_count;
map<string,int>ma={
{
"北京",0}, {
"天津",1}, {
"上海",2},{
"重庆",3},{
"石家庄",4},
{
"太原",5},{
"西安",6}, {
"济南",7},{
"郑州",8},{
"沈阳",9},{
"长春",10},
{
"哈尔滨",11}, {
"南京",12},{
"杭州",13},{
"合肥",14},{
"南昌",15},{
"福州",16},
{
"武汉",17}, {
"长沙",18},{
"成都",19},{
"贵阳",20},{
"昆明",21},{
"广州",22},
{
"海口",23}, {
"兰州",24},{
"西宁",25},{
"台北",26},{
"呼和浩特",27},{
"乌鲁木齐",28},
{
"拉萨",29}, {
"南宁",30},{
"银川",31},{
"香港",32},{
"澳门",33}};
char ss[50][50]={
"北京","天津","上海","重庆","石家庄","太原","西安","济南","郑州","沈阳","长春","哈尔滨","南京","杭州","合肥","南昌","福州","武汉","长沙","成都","贵阳","昆明","广州","海口","兰州","西宁","台北","呼和浩特","乌鲁木齐","拉萨","南宁","银川","香港","澳门"};
map<double ,string>ma2;
typedef pair<double,double>pii;
typedef pair<int,int>pii2;
map<double ,pii>pos;//实际坐标
map<int,pii2>pos2;//图上的相对坐标
using namespace std;
typedef struct path{
int route[MAX_CITY_NUM];
double dis=0;
}path;
//path里边有当前迭代过程的最有路径和最优距离
double w[MAX_CITY_NUM][MAX_CITY_NUM];
int num;
double s = 0;
path p0;
void Init_pos()//初始化距离
{
pos[0]={
3639,1315};
pos[1]={
3712,1399};
pos[2]={
4177,2244};
pos[3]={
2769,2492