动态规划,tsp 问题,15 个城市中有 9 个

如何解决动态规划,tsp 问题,15 个城市中有 9 个

我想我有某种 TSP 问题。我有 15 个城市之间的距离矩阵:

  A B C D E F G H I J K L M N O
A 0 3 8 7 8 9 4 4 2 9 5 5 7 9 9
B 9 0 6 3 8 9 3 9 5 3 3 4 8 6 8
C 1 7 0 8 3 5 4 3 1 1 7 8 2 4 3
D 1 9 7 0 4 3 5 6 8 4 3 4 2 8 9
E 5 8 3 5 0 9 7 4 9 4 5 7 4 6 2
F 5 7 9 6 2 0 3 5 3 6 6 7 4 9 2
G 3 2 8 1 1 8 0 3 4 5 2 4 7 2 6
H 1 4 7 5 5 3 8 0 1 1 7 6 5 8 1
I 5 5 6 5 5 6 6 4 0 2 1 3 4 9 5
J 4 5 4 1 3 9 2 7 9 0 6 8 1 9 9
K 3 4 6 5 9 4 9 5 2 5 0 5 1 4 2
L 8 9 5 2 6 2 9 9 4 5 5 0 3 1 5
M 5 9 7 1 5 5 5 4 6 2 1 6 0 9 2
N 9 5 7 5 7 8 6 5 2 7 1 2 9 0 1
O 7 6 9 6 9 8 4 5 6 2 9 7 7 7 0

A 到 B 的距离与 B 到 A 的距离不同。
行中的字母表示来自
的城市 列中的字母表示城市到
示例:
A到F的距离是9
从 F 到 A 的距离是 5

我必须在A城市开始和结束。我必须去9个不同的城市,我不能两次访问同一个城市。行驶距离应最小化。我熟悉 TSP 算法,但我不确定如何仅针对 9 个城市执行此操作。应该可以只使用一次 tsp 算法来解决这个问题。感谢您的帮助。

解决方法

我终于明白了:

// Dynamic Programming based Java program to find shortest path with
// exactly k edges
import java.util.*;
import java.lang.*;
import java.io.*;

class knapsack{

// Define number of vertices in the graph and inifinite value
static final int V = 15;
static final int INF = Integer.MAX_VALUE;
static int numberofedges=10;
static int[][][] S=new int[15][15][15];


// A Dynamic programming based function to find the shortest path
// from u to v with exactly k edges.
static <bolean> int shortestPath(int graph[][],int u,int v,int k)
{   for(int y=0;y<15;y++){
    for(int x=0;x<15;x++){
        for(int z=0;z<9;z++){
    S[x][y][z]=-1;
}}}
    // Table to be filled up using DP. The value sp[i][j][e] will
    // store weight of the shortest path from i to j with exactly
    // k edges
    int sp[][][] = new int[V][V][k+1];
    System.out.println(Arrays.toString(S));
    // Loop for number of edges from 0 to k
    for (int e = 0; e <= k; e++)
    {
        for (int i = 0; i < V; i++)  // for source
        {
            for (int j = 0; j < V; j++) // for destination
            {
                sp[i][j][e] = INF;

                boolean gofind=true;

                for (int x = 1; x <= e; x++) {


                    if (S[i][j][x] == j) {
                        System.out.println(S[i][j][x]);
                        System.out.println("TU");
                        gofind = false;
                        break;
                    }

            }

                if(gofind){
                // initialize value


                // from base cases
                if (e == 0 && i == j) {
                    S[i][j][0]=0;
                    sp[i][j][e] = 0;
                }
                    if (e == 1 && graph[i][j] != INF) {
                        S[i][j][e]=j;
                        sp[i][j][e] = graph[i][j];
                    }
                // go to adjacent only when number of edges is
                // more than 1
                if (e > 1)
                {int help=225;
                    for (int a = 0; a < V; a++)
                    {
                        // There should be an edge from i to a and
                        // a should not be same as either i or j
                        if (graph[i][a] != INF && i != a &&j!= a && sp[a][j][e-1] != INF)
                        {if(sp[i][j][e]>graph[i][a] + sp[a][j][e-1]){
                            help=a;


                        }
                            sp[i][j][e] = Math.min(sp[i][j][e],graph[i][a] + sp[a][j][e-1]);
                        if(help>16)
                            S[i][j][e]=j;
                            else
                            S[i][j][e]=a;
                    }}
                }
            }}
        }
    }
    return sp[u][v][k];
}

public static void main (String[] args)
{
    try {
    Scanner sc = null;
    sc = new Scanner(new BufferedReader(new FileReader("src/ADS2021_cvicenie5data.txt")));
    /* Let us create the graph shown in above diagram*/
    int[][] graph = new int[15][15];
        sc.nextLine();
    while(sc.hasNextLine()) {
            for (int i=0; i<graph.length; i++) {
                String[] line = sc.nextLine().trim().split(" ");
                for (int j=0; j<line.length; j++) {
                    graph[i][j] = Integer.parseInt(line[j]);
                }
            }
        }
    System.out.println(Arrays.deepToString(graph));

    System.out.println("Weight of the shortest path is "+ shortestPath(graph,numberofedges));
    System.out.println(Arrays.toString(S));
}
catch (
FileNotFoundException e) {
e.printStackTrace();
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?