单源最短路径,关于这个问题的贪心算有点不好理解,分析后续补充,代码也需要后续优化,便于理解

package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by saishangmingzhu on 2018/12/3.
 * 单源最短路径
 */
public class SingleSourceShortestPath {
    public static void main(String[] arg) {
        new SingleSourceShortestPath().greedy();
    }

    /**
     * 贪心算法
     */
    public void greedy(){
        //【1】创建有向图
        List pointList=new ArrayList<>();
        pointList.add(new Point("A",0));
        pointList.add(new Point("B",1));
        pointList.add(new Point("C",2));
        pointList.add(new Point("D",3));
        pointList.add(new Point("E",4));
        Map pathMap=new HashMap<>();
        pathMap.put("AB",10);
        pathMap.put("AD",30);
        pathMap.put("AE",100);
        pathMap.put("BC",50);
        pathMap.put("CE",10);
        pathMap.put("DC",20);
        pathMap.put("DE",60);
        //【2】从源顶点计算距离
        // 源顶点为A
        int[] dist=new int[pointList.size()];
        for (int i=1;i resultList=new ArrayList<>();
        Point first=pointList.get(0);
        pointList.remove(0);
        while (pointList.size()>0){
            int min=10000;
            Point minP=null;
            for (int i=0;i v + dist[first.getIndex()]) {
                        dist[p.getIndex()] = v + dist[first.getIndex()];
                        if (min>v + dist[first.getIndex()]){
                            min=v + dist[first.getIndex()];
                            minP=p;
                        }
                    }
                }
                else {
                    if (min>dist[p.getIndex()]){
                        min=dist[p.getIndex()];
                        minP=p;
                    }
                }
            }
            resultList.add(minP);
            pointList.remove(minP);
            first=minP;

        }
        for (int i:dist)
            System.out.println(i);
    }
}
class Point{
    String name;
    int index;

    public Point(String name, int index) {
        this.name = name;
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}