计应 191 西 杜旭晖
public class Dijkstra {
public static int max = 99999;
public static void main(String[] args) {
int[][] graph = {{0, 4, 6, 6, max, max, max},
{max, 0, 1, max, 7, max, max},
{max, max, 0, max, 6, 4, max},
{max, max, 2, 0, max, 5, max},
{max, max, max, max, 0, max, 6},
{max, max, max, max, 1, 0, 8},
{max, max, max, max, max, max, max}};
int startPointIndex = 0;
int[] dis = dijkastra(graph, startPointIndex);
System.out.println(dis.toString());
}
public static int[] dijkastra(int[][] graph, int startPointIndex) {
boolean[] s = new boolean[graph.length];//集合s表示,已经求出最短路径的点的集合
for (int i = 0; i < graph.length; i++) {//初始化s集合,只有起始点
if (i == startPointIndex) {
s[i] = true;
} else {
s[i] = false;
}
}
int[] dis = new int[graph.length];//最短路径集合
for (int i = 0; i < graph.length; i++) {//初始化,起始点到其他点的距离。
dis[i] = graph[startPointIndex][i];
}
for (int i = 0; i < graph.length; i++) {//求到每个点的最短路径
int tmpdis = max;
int tmpindex = 0;
for (int j = 0; j < graph.length; j++) {
if (!s[j] && dis[j] < tmpdis) {//不在s集合(未求出该店的最短路径)
tmpdis = dis[j];
tmpindex = j;
}
}
//dis中距离最短的点加入到s集合中
s[tmpindex] = true;
for (int j = 0; j < graph.length; j++) {//更新起点到其他点距离
if (dis[j] > dis[tmpindex] + graph[tmpindex][j]) {
dis[j] = dis[tmpindex] + graph[tmpindex][j];
}
}
}
return dis;//返回起点到每个点的最短路径长度
}
public static class CharGing
{
public static double CharGing1(double x)
{
int def = 2;
if (x>=0 && x <= 6)
{
return def;
}
else if (6 < x && x <= 13)
{
return def = def + 1;
}
else if (13 < x && x <= 21)
{
return def = def + 1 + 1;
}
else if (x > 21)
{
var tmp = Math.Round((x - 21) / 9,1);
return tmp = tmp + def + 1 + 1;
}
return 0;
}
}
}

浙公网安备 33010602011771号