public class Dijkstra {
ArrayList<Node> Collection = new ArrayList<Node>(); // 储存所有的点
ArrayList<Node> Route = new ArrayList<Node>(); // 记录路径
ArrayList<Node> Waiting = new ArrayList<Node>();// 此轮候选的点
String current = null;
int length = 0;
boolean addNodes() {
Collection.add(new Node("A", "B", 20));
Collection.add(new Node("A", "D", 80));
Collection.add(new Node("A", "G", 90));
Collection.add(new Node("B", "F", 10));
Collection.add(new Node("C", "F", 50));
Collection.add(new Node("C", "H", 20));
Collection.add(new Node("C", "D", 10));
Collection.add(new Node("D", "G", 20));
Collection.add(new Node("D", "C", 10));
Collection.add(new Node("E", "B", 50));
Collection.add(new Node("E", "G", 30));
Collection.add(new Node("F", "C", 10));
Collection.add(new Node("F", "D", 40));
Collection.add(new Node("G", "A", 20));
return true;
}
void setNext(String nt) {
current = nt;
}
void update(ArrayList<Node> wl) {
ArrayList<Node> update = new ArrayList<Node>();
for (Node a : wl) {
for (Node b : wl) {
if (a.getDest() == b.getDest() && a.getDist() < b.getDist()) {
update.add(b);
}
}
}
for (Node c : update) {
wl.remove(c);
}
}
void Remove(ArrayList<Node> coll, String nt) {
ArrayList<Node> move = new ArrayList<Node>();
for (Node a : coll) {
if (nt == a.getDest())
move.add(a);
}
for (Node a : move) {
if (coll.contains(a))
coll.remove(a);
}
}
void linkRoute(ArrayList<Node> rt) {
ArrayList<Node> linkroute = new ArrayList<Node>();
int last = rt.size() - 1;
for (int i = rt.size() - 2; i > -1; i--) {
if (rt.get(last).getDep() != rt.get(i).getDest()) {
linkroute.add(rt.get(i));
} else if (rt.get(last).getDep() == rt.get(i).getDest()) {
last = i;
}
}
for (Node a : linkroute) {
rt.remove(a);
}
}
void toWait(String nt) {
Remove(Collection, current);
for (Node a : Collection) {
if (nt == a.getDep()) {
Waiting.add(new Node(a.getDep(), a.getDest(), a.getDist() + length));
}
}
for (Node b : Waiting) {
if (Collection.contains(b))
Collection.remove(b);
}
update(Waiting);
}
boolean selectNext(ArrayList<Node> wl) {
if (wl.size() == 0) {
System.out.println("done");
return false;
} else if (wl.size() == 1) {
current = wl.get(0).getDest();
length = wl.get(0).getDist();
Route.add(Route.size(), wl.get(0));
wl.remove(wl.get(0));
} else {
Node s = wl.get(0);
for (Node a : wl) {
if (a.getDist() < s.getDist()) {
s = a;
}
}
current = s.getDest();
length = s.getDist();
Route.add(Route.size(), s);
wl.remove(s);// 将waiting list中的这一点删去
}
return true;
}
public void wrap(String start, String stop) {
current = start;
if (addNodes()) {
for (int i = 0;; i++) {
if (stop == current) {
// Route.add();
break;
}
toWait(current);
if (selectNext(Waiting))
continue;
else
break;
}
if (Route.size() != 0 && Route.get(Route.size() - 1).getDest() == stop) {
linkRoute(Route);
System.out.print(Route.get(0).getDep() + " -> ");
for (Node a : Route) {
System.out.print(a.getDest() + " ");
}
System.out.print("distance: " + Route.get(Route.size() - 1).getDist() + "\n");
} else
System.err.println("no fucking way");
}
}
public static void main(String[] args) {
new Dijkstra().wrap("A", "G");
}
}
class Node {
String From;
String To;
int distance;
Node(String f, String t, int d) {
this.From = f;
this.To = t;
this.distance = d;
}
public String getDep() {
return this.From;
}
public String getDest() {
return this.To;
}
public int getDist() {
return this.distance;
}
}