TSP:旅行者问题(动态规划+递归)
2011-10-08 21:02
176 查看
// TSP.cpp : Defines the entry point for the console application. // #include "stdafx.h" typedef struct _node_st{ bool inPath ; int nextIndex; _node_st():inPath(false),nextIndex(-1){ } }Node,*NodePtr; #define MAX 6 int dis[MAX][MAX]={ 0, 10, 20, 30, 40, 50, 12, 0 ,18, 30, 25, 21, 23, 19, 0, 5, 10, 15, 34, 32, 4, 0, 8, 16, 45, 27, 11,10, 0, 18, 56, 22, 16,20, 12, 0 }; Node p[MAX]; void printPath(); //表示从begin开始,经过所有点后到达end的最小距离 int tsp(int beginIndex,int endIndex){ int result = 0xFFFFFFF; bool isLast = true; for(int i=0;i<MAX;i++){ if(i!=beginIndex){ if(!p[i].inPath){ isLast = false; p[i].inPath = true; int value = tsp(i,endIndex); if(dis[beginIndex][i]+value<result){ p[beginIndex].nextIndex = i; result = dis[beginIndex][i]+value; if(beginIndex==0&&endIndex==0){ printPath(); } } p[i].inPath = false; } } } if(isLast){ p[beginIndex].nextIndex = endIndex; return dis[beginIndex][endIndex]; } return result; } void printPath(){ int pre = 0,next; int count = MAX; while(count--){ next = p[pre].nextIndex; printf("%d->%d ",pre,next); pre = next; } printf("\n"); } int main(int argc, char* argv[]) { //freopen("i://out.txt","w",stdout); p[0].inPath = true; printf("%d\n",tsp(0,0)); return 0; }
相关文章推荐
- TSP(旅行者问题)——动态规划详解
- TSP(旅行者问题)——动态规划详解
- 在matlab上实现遗传算法解决TSP旅行者问题
- POJ 3311 Hie with the Pie(TSP问题 状压DP)
- 遗传算法解决TSP问题(C++)
- 图论算法(4) --- TSP旅行商问题 求最短回路(acm)
- 蚁群算法(Java)tsp问题
- 旅行商问题 TSP问题 状压dp入门 + floyd poj 3311 hdu 5418
- 遗传算法解决TSP问题(C++)
- TSP问题的解决方法
- 蚁群算法求解TSP问题的源代码
- 遗传算法及TSP问题
- 遗传算法(GA)学习笔记---旅行商问题(TSP)
- TSP_旅行商问题 - 蛮力法DFS(一)
- TSP问题
- 用遗传算法解决tsp问题
- 遗传算法GA及其在旅行商问题TSP上的研究
- 免疫算法求解TSP问题
- 基于蚁群算法求解求解TSP问题(JAVA)
- 基于粒子群算法求解TSP问题(JAVA)