迪杰斯特拉算法处理有向图中最短路径的(dijkstra)Java实现及升级

程序中要用到最短路径的寻找,就用了迪杰斯特拉算法,在网上找了个实现,然后自己又升了下级,如下:

package reverse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;


public class DijSuccess {
public static int INFINITY = 99999;
    public static Map<String,Vertex> vertexMap = new HashMap<String,Vertex>();
    
    //边距
    static class Edge{
        public Vertex dest;
        public double cost;
        public Edge(Vertex d,double c){
            this.dest = d;
            this.cost = c;
        }
        
    }
    
    //静态类:Vertex
    static class Vertex implements Comparable<Vertex>{
        public String name;
        public List<Edge> adj;
        public double dist;
        public Vertex prev;
        public int scratch;
        public boolean visited;
        public Vertex(String nm){
            this.name = nm;
            adj = new ArrayList<Edge>();
            reset();
        }
        public void reset(){
            visited = false;
            dist=DijSuccess.INFINITY;
        }
        @Override
        public int compareTo(Vertex o) {
            double c = o.dist;
            
            return dist < c ? -1:dist > c ? 1:0;
        }
        
    }
    
    //dijkstra算法实现:找到从startName点出发,到其他所有点的最短路径:选取自己定义的终点
    public static void dijkstra(String startName,String endName){
        PriorityQueue<Vertex> pq = new PriorityQueue<Vertex>();//该队列以权值升序排列,因为Vertex实现Comparable接口
        Vert

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值