- 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<Integer, Integer> thisPointMap = new TreeMap<Integer, Integer>();// 該點到各點的距離。
- 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> point_arr = new ArrayList<Point>();// 存儲點集合
- 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<Point> 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<Point> 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;
- }
- }
- <p>運行結果:</p>
- 請輸入頂點個數: 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