【回溯法】旅行售货员问题

【回溯法】旅行售货员问题

问题描述

某售货员要到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];
                }
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值