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

http://blog.csdn.net/rookie_algo/article/details/7391887

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为止

代码:

[java]  view plain copy print ?
  1. import java.util.*;  
  2. public class DjistraPath {  
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     /* 
  7.      * 本题是求从0点到所有点的最短路径 
  8.      * 每一轮"Find shrotest"都是再找距离0点最近的点v,顾可以知道暂时从0到v的最短距离就是dist[v],因为如果还有更近的距离,那么v就不是距离0最近的点 
  9.      * 到找到最近点v后,都要以v为过度点,来比较0->v->j和原来记录的路径哪个更近,从而以刷新dist[] 
  10.      * 当假设除了0点其他,都当过v点的所有情况后,dist[j]数组保留下来的就是从0到j的最短路径 
  11.      */  
  12.     final static int MAXN = 100;  
  13.     final static int BigNum = 10000000;  
  14.     static Scanner scan = new Scanner(System.in);  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.         int[][] graph = new int[MAXN][MAXN];//The Adjacency matrix of the graph  
  18.         int[] dist = new int[MAXN];//The shortest distence between 0 and N  
  19.         boolean[] vis= new boolean[MAXN];//Sign the point which is visited  
  20.         int n,m;//n stands for theamount of positions;m stands for the path  
  21.         n = scan.nextInt();  
  22.         m = scan.nextInt();  
  23.         Arrays.fill(vis, false);  
  24.         for(int i=0;i<n;i++)      
  25.             for(int j=0;j<n;j++)      
  26.                 if(i==j)  
  27.                     graph[i][j] = 0;  
  28.                 else  
  29.                     graph[i][j] = BigNum;  
  30.         int pos1,pos2;  
  31.         for(int i=0;i<m;i++)    {//Set the date  
  32.             pos1 = scan.nextInt();  
  33.             pos2 = scan.nextInt();  
  34.             graph[pos1][pos2] = scan.nextInt();  
  35.         }  
  36.         for(int i=0;i<n;i++)    //Set the dist[]  
  37.             dist[i] = graph[0][i];  
  38.         vis[0] = true;int min,v = 0;  
  39.         for(int i=0;i<n-1;i++)    {//Check n-1 times  
  40.             min = BigNum;  
  41.             for(int j=0;j<n;j++)    {//Find shortest  
  42.                 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  
  43.                     min = dist[j];  
  44.                     v = j;  
  45.                 }  
  46.             vis[v] = true;        //Sign the point v to be visited   
  47.             }  
  48.             for(int j=0;j<n;j++)    {//Refresh the dist[]  
  49.                 if(vis[j] != true && dist[j]>dist[v]+graph[v][j])    {//when distence is shorter when pass the point v refresh the dist  
  50.                     dist[j] = dist[v] + graph[v][j];  
  51.                 }  
  52.             }  
  53.         }  
  54.         for(int i=0;i<n;i++)    {  
  55.             System.out.println("0->"+i+":"+dist[i]);  
  56.         }  
  57.     }  
  58. }  
  59. /* 
  60. Test Date: 
  61. 5 7 
  62. 0 1 3 
  63. 0 3 8 
  64. 1 2 5 
  65. 1 4 4 
  66. 2 3 4 
  67. 2 4 7 
  68. 3 4 2 
  69. Out put: 
  70. 0->1:3 
  71. 0->2:8 
  72. 0->3:8 
  73. 0->4:7 
  74. */  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值