java实现加权抽样_java - Java实现加权图? - SO中文参考 - www.soinside.com

我编写了我的代码,但不知道如何访问图形的权重,或者如何在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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值