A星算法解决TSP问题
来源:互联网 发布:sql insert into 编辑:程序博客网 时间:2024/06/03 19:52
TSP问题相信大部分人已经很熟悉了,就直接展示解法了
#include<iostream>#include<cmath>#include <vector>using namespace std;double dis(int a[], int b[]){ double x1 = a[0]; double y1 = a[1]; double x2 = b[0]; double y2 = b[1]; double x = pow(x1 - x2, 2); double y = pow(y1 - y2, 2); return sqrt(x + y);}//double f(int start, int next){////}int main(){ int city[10][2]; for (int i = 0; i < 10; i++) for (int j = 0; j < 2; j++) cin >> city[i][j]; int start; cin >> start;//start-1 = 起始 double discity[10][10];//距离矩阵 for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) { if (i == j) discity[i][j] = 999; else discity[i][j] = dis(city[i], city[j]); } int step[20];//用于存储已访问过的城市 int arrived[10] = { 0 }; step[0] = start - 1; double sum = 0; int cur = step[0]; arrived[step[0]] = 1; int next; int count = 1; while (count<10) { cur = step[count - 1]; int min = 9999; for (int i = 0; i < 10; i++){ if (arrived[i] == 0) { if (min > discity[cur][i] + discity[start][i]) { min = discity[cur][i] + discity[start][i]; next = i; } } } arrived[next] = 1; sum += discity[cur][next]; //cout << sum << endl; //cout << discity[cur][next] << endl; step[count] = next; count++; //cout << sum<<endl; } sum += discity[cur][next]; for (int i = 0; i < 10;i++) { cout << step[i] +1<<"->"; } cout << start << endl; cout << sum << endl; //cout << "No output." << endl; system("pause"); return 0; //1 2 3 7 4 9 5 1 2 8 4 6 3 8 9 1 8 8 4 4 //5}
0 0
- A星算法解决TSP问题
- 遗传算法解决TSP问题
- 遗传算法解决tsp问题
- 遗传算法解决TSP问题
- 遗传算法解决TSP问题
- 遗传算法解决TSP问题
- 遗传算法解决tsp问题
- 退火算法解决tsp问题
- 贪心算法解决tsp问题
- 遗传算法解决TSP问题
- 遗传算法解决TSP问题
- 用遗传算法解决TSP问题
- 遗传算法解决网络布线问题 tsp
- 模拟退火算法解决TSP问题
- 使用遗传算法解决TSP问题
- 模拟退火算法解决TSP问题
- 模拟退火算法解决TSP问题
- 利用遗传算法解决TSP问题
- supervisor运行golang守护进程
- Storm之Toplogy编程
- Tanks Unity Tutorial
- HBase优化之bulkload写入
- VS2010 警告1warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s
- A星算法解决TSP问题
- 七种Mysql表类型
- ARM的字对齐问题总结
- 基于Java的webservice创建与soap方式调用
- android系统架构详解
- 微服务,微架构[二]之springboot集成jdbc
- 安装APK的错误码(PackageManager.java)
- Stome之接受上游数据
- SQLite使用小结