我编写了我的代码,但不知道如何访问图形的权重,或者如何在main方法中打印它的边缘,请查看我的代码。请帮助,实际上我试图实现Dijkstra,但我不知道这是在图表中包含权重的正确方法。请帮助尝试解决过去三天。
public class Gr {
public class Node{
public int vertex;
public int weight ;
public int getVertex() {return vertex;}
public int getWeight() {return weight;}
public Node(int v , int w){
vertex=v;
weight=w;
}
}
private int numVertices=1 ;
private int numEdges=0 ;
private Map> adjListsMap= new HashMap<>();
public int getNumVertices(){
return numVertices;
}
public int addVertex(){
int v = getNumVertices();
ArrayList neighbors = new ArrayList<>();
adjListsMap.put(v,neighbors);
numVertices++ ;
return (numVertices-1);
}
//adding edge
public void addEdge(int u , int v,int w ){
numEdges++ ;
if(v
(adjListsMap.get(u)).add( new Node(u,w));
(adjListsMap.get(v)).add(new Node(u,w));
}
else {
throw new IndexOutOfBoundsException();
}
}
//getting neighbours
public List getNeighbors(int v ){
return new ArrayList<>(adjListsMap.get(v));
}
public static void main(String[] args){
Gr g = new Gr();
for(int j=1;j<=3;j++)
g.addVertex();
for(int k =1;k<=2;k++)
{ int u= in.nextInt();
int v = in.nextInt();
int w = in.nextInt();
g.addEdge(u,v,w);
}
}
}