最近在复习java,下学期要用,写这个练手. 技术较粗糙,见谅.
代码里用的是这幅地图,根据实际情况更改,在addNode方法中
这个是运行结果,起点和终点在 运行wrap(String qidian, String zhongdian) 时定义
代码:
import java.util.*;
public class Dijkstra {
ArrayList Collection = new
ArrayList(); //储存所有的点
ArrayList Route = new
ArrayList(); //记录路径
ArrayList Waiting = new
ArrayList();//此轮候选的点
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
wl){
ArrayList update = new
ArrayList();
for(Node a:wl){
for(Node b:wl){
if(a.getDest() ==
b.getDest()&&a.getDist()
update.add(b);
}}}
for(Node c:update){
wl.remove(c);
}
}
void Remove(ArrayList
coll,String nt){
ArrayList move = new
ArrayList();
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
rt){
ArrayList linkroute = new
ArrayList();
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
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 = 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;
}
}