旅行商问题

旅行商问题:一个商人从城市A出发,访问BCDE等城市各一次最后回到A,问行程如何使得路程或费用最低。

这是个NP(非多项式可解,但一般验证容易)问题,假设中间有4个城市,那么全排列为4!=24种,没有很好的算法,基本只能穷举了。


class Vertex_4 {
    char label;

    public Vertex_4(char label) {
        this.label = label;
    }
}

public class lianxi {
    private final int maxSize = 20;
    private final int INF = 100000;
    private int nvers;
    private int[][] adjMat;
    private Vertex_4[] vertex;

    public lianxi() {
        nvers = 0;
        vertex = new Vertex_4[maxSize];
        adjMat = new int[maxSize][maxSize];
        for (int i = 0; i < maxSize; i++)
            for (int j = 0; j < maxSize; j++)
                adjMat[i][j] = INF;
    }

    public void addVer(char label) {
        vertex[nvers++] = new Vertex_4(label);
    }

    // 无向
    public void addEdge(int s, int e, int d) {
        adjMat[s][e] = d;
        adjMat[e][s] = d;
    }

    public void findRoad() {
        String s = "";
        for (int i = 0; i < nvers; i++)
            s = s + vertex[i].label;
        find(s, "");
    }

    // 列出所有排列,然后计算距离和
    private void find(String s, String result) {
        if (s.length() == 0) {
            int sum = 0;
            char[] array = result.toCharArray();
            for (int i = 0; i < array.length - 1; i++) {
                sum += adjMat[(int) array[i] - 65][(int) array[i + 1] - 65];// char转回数字-65
            }
            sum += adjMat[(int) array[0] - 65][(int) array[array.length - 1] - 65];
            result += vertex[(int) array[0] - 65].label;
            if (sum < INF)
                System.out.println(result + " : " + sum);
            // else System.out.println(result+" : inf");
        } else {
            for (int i = 0; i < s.length(); i++) {
                find(s.substring(1), result + s.charAt(0));
                s = s.substring(1) + s.charAt(0);
            }
        }
    }

    public static void main(String[] args) {
        lianxi g = new lianxi();
        g.addVer('A');
        g.addVer('B');
        g.addVer('C');
        g.addVer('D');
        g.addVer('E');
        g.addEdge(0, 1, 10);
        g.addEdge(0, 2, 30);
        g.addEdge(0, 3, 50);
        g.addEdge(1, 2, 40);
        g.addEdge(1, 4, 20);
        g.addEdge(2, 3, 60);
        g.addEdge(2, 4, 40);
        g.addEdge(3, 4, 70);
        g.findRoad();//输出所有可能路径的距离和
    }

}

也就是说这是个递归的问题,感觉递归比较抽象,理解起来稍费劲。附上一个对字符串数组部分序列全排的程序,比上面的要好。

public class lianxi2 {
    public static int total = 0;

    public static void swap(String[] str, int i, int j) {
        String temp = new String();
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }

    public static void arrange(String[] str, int i, int j) {
        arrange(str, i, j, i);
    }

    // 字符串数组的部分序列全排
    // 最后一个参数只是保存起始点的作用,第二个参数与第四个需保持一致
    private static void arrange(String[] str, int st, int len, int temp) {
        if (st == len - 1) {
            for (int i = temp; i < len; i++)
                System.out.print(str[i] + "  ");
            System.out.println();
            total++;
        } else {
            for (int i = st; i < len; i++) {
                swap(str, st, i);// 从起始点开始,每次都将新的字符串交换至序列开头
                arrange(str, st + 1, len, temp);// 对除开头外的序列递归
                swap(str, st, i);// 这个作用是假设排列abcd,第一次ab互换,余下递归。那么第二次要从abcd重新开始将ac互换的,所以递归后要还原字符串数组。
            }
        }

    }

    public static void main(String[] args) {
        String str[] = { "a", "b", "c", "d" };
        arrange(str, 0, 4);
        System.out.println("total: " + total);
    }

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值