JAVA编程求单源最短路径_用java编写的一个迪杰斯特拉算法(单源最短路径算法,Dijkstra算法)。...

可以用于有向图和无向图。用负数表示该有向路不通。在EditPlus上写的,所以就一个.java文件。

package Test;

import java.util.TreeMap;

import java.util.ArrayList;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

class Point {

private int id;// 点的id

private boolean flag = false;// 标志是否被遍历

int sum;// 记录总的点个数

private TreeMap thisPointMap = new TreeMap();// 该点到各点的距离。

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

Point(int sum) { // 构造函数 带有顶点个数

this.sum = sum;

}

public void setId(int id) {// 设置顶点id

this.id = id;

}

public int getId() {// 获得顶点id

return this.id;

}

public void changeFlag() {// 修改访问状态。

this.flag = true;

}

public boolean isVisit() {// 查看访问状态

return flag;

}

public void setLenToOther()throws IOException{// 初始化改点到各顶点的距离。

System.out.println("=======请输入顶点" + (this.id + 1) + "至其他各顶点的边距=======");

for (int i = 0; i < sum; i++) {

if (i == this.id)

thisPointMap.put(this.id, 0);

else {

System.out.print("至 顶点" + (i + 1) + " 的距离 :");

boolean flag =true;

int len = 0;

while(flag){

try {

len = Integer.valueOf(bufr.readLine());

flag = false;

} catch (NumberFormatException e) {

System.out.print("输入有误,请重新输入:");

}

};

thisPointMap.put(i, len);

}

}

}

// 该点到顶尖id的 距离。

public int lenToPointId(int id) {

return thisPointMap.get(id);

}

}

class Dijkstra {

public static void main(String[] args)throws IOException {

ArrayList point_arr = new ArrayList();// 存储点集合

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

System.out.print("请输入顶点个数: ");

int sum = 0;

boolean flag =true;

while(flag){

try {

sum = Integer.valueOf(bufr.readLine());

flag = false;

} catch (NumberFormatException e) {

System.out.print("输入有误,请重新输入:");

}

};

for (int i = 0; i < sum; i++) {// 初始化

Point p = new Point(sum);

p.setId(i);

p.setLenToOther();

point_arr.add(p);

}

System.out.print("请输入起始顶点 id :");

boolean flag2 =true;

int start = 0;

while(flag2){

try {

start = Integer.valueOf(bufr.readLine())-1;

if(start > sum-1 || start < 0)

throw new NumberFormatException();

flag2 = false;

}catch (NumberFormatException e) {

System.out.print("输入有误,请重新输入:");

}

};

showDijkstra(point_arr, start);// 单源最短路径遍历

}

public static void showDijkstra(ArrayList arr, int i) {

System.out.print("顶点" + (i + 1));

arr.get(i).changeFlag();

Point p1 = getTopointMin(arr, arr.get(i));

if (p1 == null)

return;

int id = p1.getId();

showDijkstra(arr, id);

}

public static Point getTopointMin(ArrayList arr, Point p) {

Point temp = null;

int minLen = Integer.MAX_VALUE;

for (int i = 0; i < arr.size(); i++) {

// 当已访问 或 者是自身或者无该路径时跳过。

if (arr.get(i).isVisit() || arr.get(i).getId() == p.getId() || p.lenToPointId(i) < 0)

continue;

else {

if (p.lenToPointId(i) < minLen) {

minLen = p.lenToPointId(i);

temp = arr.get(i);

}

}

}

if (temp == null)

return temp;

else

System.out.print(" @--" + minLen + "--> ");

return temp;

}

}

运行结果:

请输入顶点个数: 5

=======请输入顶点1至其他各顶点的边距=======

至 顶点2 的距离 :15

至 顶点3 的距离 :24

至 顶点4 的距离 :33

至 顶点5 的距离 :28

=======请输入顶点2至其他各顶点的边距=======

至 顶点1 的距离 :16

至 顶点3 的距离 :25

至 顶点4 的距离 :18

至 顶点5 的距离 :21

=======请输入顶点3至其他各顶点的边距=======

至 顶点1 的距离 :29

至 顶点2 的距离 :30

至 顶点4 的距离 :25

至 顶点5 的距离 :29

=======请输入顶点4至其他各顶点的边距=======

至 顶点1 的距离 :31

至 顶点2 的距离 :36

至 顶点3 的距离 :22

至 顶点5 的距离 :11

=======请输入顶点5至其他各顶点的边距=======

至 顶点1 的距离 :-1

至 顶点2 的距离 :-1

至 顶点3 的距离 :30

至 顶点4 的距离 :39

顶点1  @--15--> 顶点2  @--18--> 顶点4  @--11--> 顶点5  @--30--> 顶点3

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值