【回溯法】旅行售货员问题
问题描述
某售货员要到4个城市去推销商品,已知各城市之间的路程,如下图所示。请问他应该如何选定一条从城市1出发,经过每个城市一遍,最后回到城市1的路线,使得总的周游路程最小。

图片来自:https://blog.csdn.net/qq_44766883/article/details/106903347
示例
输出结果:
1 3 2 4 1
思路
利用回溯法遍历各种可能,当访问一个与上一节点有路径的节点时,需要先判断添加当前路径后会不会大于当前最小路径smallCost,如果会的话,就剪枝。当添加的节点数到达n,表明此时已经经过了n个城市,这时候需要判断最后的城市和起点城市是否有路径,有路径的话添加进去会小于smallCost就更新。
代码
public class travleSeller {
static int n;
static int m;
static int [][]Graph;
static int smallCost = 100000;
static int curCost;
static int best[];
static int visited[]; //记录是否被访问过
static int path[]; //记录路径
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt(); //路径数量
Graph = new int[n+1][n+1];
visited = new int[n+1];
path = new int[n+1];
best = new int[n+1];
for (int i=0;i<=n;++i){
for(int j=0;j<=n;++j){
Graph[i][j]=0;
}
}
for(int i=0;i<=n;++i){
visited[i] = best[i] = path[i]= 0;
}
System.out.println("输入城市之间的距离(城市1 城市2 距离):");
for(int i = 0;i < m;++i){
int a = sc.nextInt();
int b = sc.nextInt();
Graph[a][b] = Graph[b][a] = sc.nextInt();
}
path[0] = path[n] = 1;
dfs(1,1);
System.out.println("smallCost: " + smallCost);
for(int i = 0;i <= n;++i){
System.out.print(best[i] + " ");
}
}
public static void dfs(int d, int curNum){
if(curNum == n && Graph[d][1] != 0){ //快到达起点
curCost += Graph[d][1];
if(curCost < smallCost){
smallCost = curCost;
for(int i = 0;i <= n;++i){
best[i] = path[i];
}
}
curCost -= Graph[d][1];
}
else{
for(int i = 2;i <= n;++i){
if(visited[i] != 0 || i == d)
continue;
if(Graph[d][i] != 0 && Graph[d][i] + curCost < smallCost){
visited[i] = 1;
path[curNum] = i;
curCost += Graph[d][i];
dfs(i,curNum+1);
visited[i] = 0;
path[curNum] = 0;
curCost -= Graph[d][i];
}
}
}
}
}
2478

被折叠的 条评论
为什么被折叠?



