import java
.util
.Scanner
;
public class zhongxing02 {
static final int MX
=1000;
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
int[][] road
=new int[][]{
{0, 1, MX
, 6, MX
},
{1, 0, 1, MX
, MX
},
{MX
, 1, 0, 3, 2},
{6, MX
, 3, 0, MX
},
{MX
, MX
, 2, MX
, 0}
};
while (sc
.hasNext()){
int n
= 5;
int bg
=sc
.nextInt();
int end
=sc
.nextInt();
System
.out
.println(djstla(road
,5,bg
-1,end
-1));
}
}
public static int djstla(int[][] road
, int n
, int b
, int e
) {
boolean[] visited
= new boolean[n
];
int[] distance
= new int[n
];
for (int i
= 0; i
< n
; i
++) {
visited
[i
] = false;
distance
[i
] = MX
;
}
visited
[b
] = true;
distance
[b
] = 0;
int cur
= b
;
int nextP
=-1;
int times
=0;
while (cur
!=e
&×
<(n
-1)) {
int minL
=Integer
.MAX_VALUE
;
for (int j
= 0; j
< n
; j
++) {
if (visited
[j
]) continue;
if (distance
[cur
] + road
[cur
][j
] < distance
[j
]) {
distance
[j
] = distance
[cur
] + road
[cur
][j
];
}
if (distance
[j
]<minL
){
minL
=distance
[j
];
nextP
=j
;
}
}
visited
[nextP
] = true;
cur
=nextP
;
times
++;
}
return distance
[e
]==MX
?-1:distance
[e
];
}
}
转载请注明原文地址:https://tech.qufami.com/read-14016.html