一、单源最短路径
①Dijkstra算法
②Floyd算法
二、Dijkstra算法
public class ExaminationD_Dijkstra {
public static final Integer infty = 999;
public static final int n = 19;
public static int getASCII(char c){
return c-'A';
}
public static int[][] init(int[][] path){
// Arrays.fill(path,infty);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) path[i][j] = path[j][i] = infty;
//☆☆☆☆☆
else path[i][j] = path[j][i] = 0;
}
}
path[getASCII('A')][getASCII('B')] = path[getASCII('B')][getASCII('A')] = 2;
path[getASCII('A')][getASCII('C')] = path[getASCII('C')][getASCII('A')] = 1;
path[getASCII('A')][getASCII('D')] = path[getASCII('D')][getASCII('A')] = 1;
path[getASCII('A')][getASCII('E')] = path[getASCII('E')][getASCII('A')] = 1;
path[getASCII('B')][getASCII('J')] = path[getASCII('J')][getASCII('B')] = 2;
path[getASCII('B')][getASCII('G')] = path[getASCII('G')][getASCII('B')] = 1;
path[getASCII('C')][getASCII('F')] = path[getASCII('F')][getASCII('C')] = 3;
path[getASCII('C')][getASCII('G')] = path[getASCII('G')][getASCII('C')] = 3;
path[getASCII('C')][getASCII('D')] = path[getASCII('D')][getASCII('C')] = 3;
path[getASCII('D')][getASCII('G')] = path[getASCII('G')][getASCII('D')] = 2;
path[getASCII('D')][getASCII('H')] = path[getASCII('H')][getASCII('D')] = 1;
path[getASCII('D')][getASCII('I')] = path[getASCII('I')][getASCII('D')] = 2;
path[getASCII('D')][getASCII('E')] = path[getASCII('E')][getASCII('D')] = 1;
path[getASCII('E')][getASCII('H')] = path[getASCII('H')][getASCII('E')] = 1;
path[getASCII('E')][getASCII('I')] = path[getASCII('I')][getASCII('E')] = 3;
path[getASCII('F')][getASCII('J')] = path[getASCII('J')][getASCII('F')] = 1;
path[getASCII('F')][getASCII('G')] = path[getASCII('G')][getASCII('F')] = 1;
path[getASCII('G')][getASCII('K')] = path[getASCII('K')][getASCII('G')] = 2;
path[getASCII('G')][getASCII('I')] = path[getASCII('I')][getASCII('G')] = 3;
path[getASCII('H')][getASCII('L')] = path[getASCII('L')][getASCII('H')] = 2;
path[getASCII('H')][getASCII('I')] = path[getASCII('I')][getASCII('H')] = 1;
path[getASCII('I')][getASCII('M')] = path[getASCII('M')][getASCII('I')] = 3;
path[getASCII('J')][getASCII('S')] = path[getASCII('S')][getASCII('J')] = 2;
path[getASCII('K')][getASCII('N')] = path[getASCII('N')][getASCII('K')] = 1;
path[getASCII('K')][getASCII('P')] = path[getASCII('P')][getASCII('K')] = 2;
path[getASCII('K')][getASCII('L')] = path[getASCII('L')][getASCII('K')] = 3;
path[getASCII('L')][getASCII('R')] = path[getASCII('R')][getASCII('L')] = 1;
path[getASCII('L')][getASCII('M')] = path[getASCII('M')][getASCII('L')] = 1;
path[getASCII('M')][getASCII('N')] = path[getASCII('N')][getASCII('M')] = 2;
path[getASCII('M')][getASCII('Q')] = path[getASCII('Q')][getASCII('M')] = 1;
path[getASCII('M')][getASCII('S')] = path[getASCII('S')][getASCII('M')] = 1;
path[getASCII('N')][getASCII('P')] = path[getASCII('P')][getASCII('N')] = 1;
path[getASCII('O')][getASCII('P')] = path[getASCII('P')][getASCII('O')] = 1;
path[getASCII('O')][getASCII('R')] = path[getASCII('R')][getASCII('O')] = 3;
path[getASCII('O')][getASCII('Q')] = path[getASCII('Q')][getASCII('O')] = 1;
path[getASCII('R')][getASCII('S')] = path[getASCII('S')][getASCII('R')] = 1;
return path;
}
public static int[] Dijkstra(int[][] path,int origin,int destination){
//辅助数组
boolean[] used = new boolean[n];
int[] shortestDistance = new int[n];
//初始化
//path[i][i]是定义为0还是infty
for (int i = 0; i < n; i++) {
shortestDistance[i] = path[origin][i];
}
used[origin] = true;
for (int i = 1; i < n; i++) {
int min = infty;
int minIndex = 0;
for (int j = 0; j < n; j++) {
if (!used[j]&&shortestDistance[j]<min) {
min = shortestDistance[j];
minIndex = j;
}
}
used[minIndex] = true;
for (int k = 0; k < n; k++) {
//☆☆☆☆☆
if (path[minIndex][k] < infty) {//阈值十分重要
if (shortestDistance[minIndex] + path[minIndex][k] < shortestDistance[k]) {
shortestDistance[k] = shortestDistance[minIndex] + path[minIndex][k];//加法超出
}
}
}
}
return shortestDistance;
}
public static void main(String[] args) {
int[][] path = new int[n][n];
int[][] afterInit = init(path);
int[] res = Dijkstra(afterInit, 0, n - 1);
for (int i = 0; i < n; i++) {
System.out.print(res[i]+"\t");
}
System.out.println(res[n-1]);
}
}
三、Floyd算法
public class ExaminationD_Floyd {
public static final Integer infty = 999;
public static final int n = 19;
public static int getASCII(char c){
return c-'A';
}
public static int[][] init(){
int[][] path = new int[n][n];
// Arrays.fill(path,infty);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) path[i][j] = path[j][i] = infty;
//☆☆☆☆☆
else path[i][j] = path[j][i] = 0;
}
}
path[getASCII('A')][getASCII('B')] = path[getASCII('B')][getASCII('A')] = 2;
path[getASCII('A')][getASCII('C')] = path[getASCII('C')][getASCII('A')] = 1;
path[getASCII('A')][getASCII('D')] = path[getASCII('D')][getASCII('A')] = 1;
path[getASCII('A')][getASCII('E')] = path[getASCII('E')][getASCII('A')] = 1;
path[getASCII('B')][getASCII('J')] = path[getASCII('J')][getASCII('B')] = 2;
path[getASCII('B')][getASCII('G')] = path[getASCII('G')][getASCII('B')] = 1;
path[getASCII('C')][getASCII('F')] = path[getASCII('F')][getASCII('C')] = 3;
path[getASCII('C')][getASCII('G')] = path[getASCII('G')][getASCII('C')] = 3;
path[getASCII('C')][getASCII('D')] = path[getASCII('D')][getASCII('C')] = 3;
path[getASCII('D')][getASCII('G')] = path[getASCII('G')][getASCII('D')] = 2;
path[getASCII('D')][getASCII('H')] = path[getASCII('H')][getASCII('D')] = 1;
path[getASCII('D')][getASCII('I')] = path[getASCII('I')][getASCII('D')] = 2;
path[getASCII('D')][getASCII('E')] = path[getASCII('E')][getASCII('D')] = 1;
path[getASCII('E')][getASCII('H')] = path[getASCII('H')][getASCII('E')] = 1;
path[getASCII('E')][getASCII('I')] = path[getASCII('I')][getASCII('E')] = 3;
path[getASCII('F')][getASCII('J')] = path[getASCII('J')][getASCII('F')] = 1;
path[getASCII('F')][getASCII('G')] = path[getASCII('G')][getASCII('F')] = 1;
path[getASCII('G')][getASCII('K')] = path[getASCII('K')][getASCII('G')] = 2;
path[getASCII('G')][getASCII('I')] = path[getASCII('I')][getASCII('G')] = 3;
path[getASCII('H')][getASCII('L')] = path[getASCII('L')][getASCII('H')] = 2;
path[getASCII('H')][getASCII('I')] = path[getASCII('I')][getASCII('H')] = 1;
path[getASCII('I')][getASCII('M')] = path[getASCII('M')][getASCII('I')] = 3;
path[getASCII('J')][getASCII('S')] = path[getASCII('S')][getASCII('J')] = 2;
path[getASCII('K')][getASCII('N')] = path[getASCII('N')][getASCII('K')] = 1;
path[getASCII('K')][getASCII('P')] = path[getASCII('P')][getASCII('K')] = 2;
path[getASCII('K')][getASCII('L')] = path[getASCII('L')][getASCII('K')] = 3;
path[getASCII('L')][getASCII('R')] = path[getASCII('R')][getASCII('L')] = 1;
path[getASCII('L')][getASCII('M')] = path[getASCII('M')][getASCII('L')] = 1;
path[getASCII('M')][getASCII('N')] = path[getASCII('N')][getASCII('M')] = 2;
path[getASCII('M')][getASCII('Q')] = path[getASCII('Q')][getASCII('M')] = 1;
path[getASCII('M')][getASCII('S')] = path[getASCII('S')][getASCII('M')] = 1;
path[getASCII('N')][getASCII('P')] = path[getASCII('P')][getASCII('N')] = 1;
path[getASCII('O')][getASCII('P')] = path[getASCII('P')][getASCII('O')] = 1;
path[getASCII('O')][getASCII('R')] = path[getASCII('R')][getASCII('O')] = 3;
path[getASCII('O')][getASCII('Q')] = path[getASCII('Q')][getASCII('O')] = 1;
path[getASCII('R')][getASCII('S')] = path[getASCII('S')][getASCII('R')] = 1;
return path;
}
public static int[][] Floyd(int[][] path,int origin,int destination){
for (int k = 0; k <n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (path[i][k] != infty&&path[k][j]!=infty) {
if (path[i][k] + path[k][j] < path[i][j]) {
path[i][j] = path[i][k] + path[k][j];
}
}
}
}
}
return path;
}
public static void main(String[] args) {
int[][] distance = init();
int[][] floyd = Floyd(distance, 0, n - 1);
for (int i = 0; i < n; i++) {
System.out.print(floyd[0][i]+"\t");
}
//0 2 1 1 1 4 3 2 3 4 5 4 5 6 7 7 6 5 6
System.out.println("\n"+floyd[0][n-1]);
}
}