import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
public class TSP {
private double[][] dArray; //距离矩阵
private int length; //距离矩阵的长度
private int lengthOfLength; //距离矩阵长度字符串的长度
private String allzero = ""; //0组成的字符串 最大值是length个(length - 1)连接起来的字符串,同样最小值是length个0连接起来
private String biggest ="";
private List<String> list = new ArrayList<String>(); //城市流列表
private Map<String, Double> store; //存储中间数据
private String notExist = "不存在";
private String firnalRoad = notExist; //最终的路径,即距离矩阵的列号取值
private String firnalCityFlow = ""; //最终形成的城市流
private String min = notExist; //最终求得的最小值
private String allFlowTime = notExist; //求解所有城市流的时间
private String guihuaTime = notExist; //动态规划的时间
/** Creates a new instance of TwentyTwo */
public TSP(double[][] dArray) {
if (this.check(dArray)) {
this.dArray = dArray;
this.length = dArray.length;
this.lengthOfLength = (length - 1 + "").length();
for (int zeroLength = 0; zeroLength < (length * lengthOfLength);) {
allzero += 0;
zeroLength = allzero.length();
}
for(int i = this.length; i > 0; i--){
this.biggest += this.toLengthOfLength(i - 1);
}
long start = System.currentTimeMillis();
this.allFlow();
long end = System.currentTimeMillis();
this.allFlowTime = end - start + "毫秒";
start = System.currentTimeMillis();
this.initstoreMap();
this.guihua(this.length - 2);
end = System.currentTimeMillis();
this.guihuaTime = end - start + "毫秒";
}
}
public String getFirnalRoad() {
return this.firnalRoad;
}
public String getFirnalCityFlow() {
if ("".equals(this.firnalCityFlow)) {
return this.notExist;
}
return this.fir
动态规划法解旅行商问题(TSP)问题的java实现
最新推荐文章于 2024-07-15 18:28:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)