迪杰斯特拉求最短路径(JAVA实现)

Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。

求最短路径步骤  

算法步骤如下:  

1. 初使时令 S={V0},T={其余顶点},T中顶点对应的距离值  

    若存在<V0,Vi>,d(V0,Vi)为<V0,Vi>弧上的权值  

    若不存在<V0,Vi>,d(V0,Vi)为∝ 

2. 从T中选取一个其距离值为最小的顶点W且不在S中,加入S

3. 对T中顶点的距离值进行修改:若加进W作中间顶点,从V0到Vi的

    距离值比不加W的路径要短,则修改此距离值  

    重复上述步骤2、3,直到S中包含所有顶点,即S=T为止

代码:

import java.util.*;
public class DjistraPath {
    /**
     * @param args
     */
    /*
     * 本题是求从0点到所有点的最短路径
     * 每一轮"Find shrotest"都是再找距离0点最近的点v,顾可以知道暂时从0到v的最短距离就是dist[v],因为如果还有更近的距离,那么v就不是距离0最近的点
     * 到找到最近点v后,都要以v为过度点,来比较0->v->j和原来记录的路径哪个更近,从而以刷新dist[]
     * 当假设除了0点其他,都当过v点的所有情况后,dist[j]数组保留下来的就是从0到j的最短路径
     */
    final static int MAXN = 100;
    final static int BigNum = 10000000;
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[][] graph = new int[MAXN][MAXN];//The Adjacency matrix of the graph
        int[] dist = new int[MAXN];//The shortest distence between 0 and N
        boolean[] vis= new boolean[MAXN];//Sign the point which is visited
        int n,m;//n stands for theamount of positions;m stands for the path
        n = scan.nextInt();
        m = scan.nextInt();
        Arrays.fill(vis, false);
        for(int i=0;i<n;i++)    
            for(int j=0;j<n;j++)    
                if(i==j)
                    graph[i][j] = 0;
                else
                    graph[i][j] = BigNum;
        int pos1,pos2;
        for(int i=0;i<m;i++)    {//Set the date
            pos1 = scan.nextInt();
            pos2 = scan.nextInt();
            graph[pos1][pos2] = scan.nextInt();
        }
        for(int i=0;i<n;i++)    //Set the dist[]
            dist[i] = graph[0][i];
        vis[0] = true;int min,v = 0;
        for(int i=0;i<n-1;i++)    {//Check n-1 times
            min = BigNum;
            for(int j=0;j<n;j++)    {//Find shortest
                if(vis[j]!= true && dist[j]<min)    {//If the point is not visited and the distence between 0 and j is smallest mark the point j
                    min = dist[j];
                    v = j;
                }
            vis[v] = true;        //Sign the point v to be visited 
            }
            for(int j=0;j<n;j++)    {//Refresh the dist[]
                if(vis[j] != true && dist[j]>dist[v]+graph[v][j])    {//when distence is shorter when pass the point v refresh the dist
                    dist[j] = dist[v] + graph[v][j];
                }
            }
        }
        for(int i=0;i<n;i++)    {
            System.out.println("0->"+i+":"+dist[i]);
        }
    }
}
/*
Test Date:
5 7
0 1 3
0 3 8
1 2 5
1 4 4
2 3 4
2 4 7
3 4 2
Out put:
0->1:3
0->2:8
0->3:8
0->4:7
*/


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值