waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢(mèng)的舞臺(tái),心有多大,舞臺(tái)有多大。踏踏實(shí)實(shí)做事,認(rèn)認(rèn)真真做人。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評(píng)論 :: 0 Trackbacks
          來源:http://blog.chinaunix.net/u1/50399/showart_405608.html

          標(biāo)題:Single-Source Shortest Paths (Dijkstra's algorithm)
          輸入:n個(gè)頂點(diǎn)的有向帶權(quán)圖G,表述為n*n矩陣。
                起始點(diǎn):v0 其取值范圍為0~n-1。
          輸出:最短路徑及其長(zhǎng)度,表述為一個(gè)二維數(shù)組path[n-1][3],每個(gè)數(shù)組元素由三個(gè)數(shù)據(jù)項(xiàng)組成,其中path[i][0]代表此最短路徑之終點(diǎn),path[i][1]代表此最短路徑之長(zhǎng)度,path[i][2]代表此最短路徑終點(diǎn)之前趨。

           

          public class ShortestPathTest
          { 
             static int[][] graph={
                 {1000, 1000, 10 , 1000, 30 , 100 ,},
                 {1000, 1000, 5 , 1000, 1000, 1000,},
                 {1000, 1000, 1000, 50 , 1000, 1000,},
                 {1000, 1000, 1000, 1000, 1000, 10 ,},
                 {1000, 1000, 1000, 20 , 1000, 60 ,},
                 {1000, 1000, 1000, 1000, 1000, 1000,},
             };
             static int [][] path;
             static int v=0;

             public static void main(String[] args)
             {
                 ShortestPath sortestPath=new ShortestPath();
                 sortestPath.input(graph, v);
                 path=sortestPath.getPath();
                 for(int i=0; path[i][1]!=1000; i++){
                     System.out.println("源點(diǎn):" + v + "; 終點(diǎn):" + path[i][0] + 
                     "; 長(zhǎng)度:" + path[i][1] + "; 終點(diǎn)前趨:" + path[i][2]);
                 }
             }
          }

          class ShortestPath
          {
              private int[][] graph;
              private int v;
              private int[][] path;
              
              void input(int[][] graph, int v)
              {
                  this.graph=graph;
                  this.v=v;
                  calculate();
              }
              
              void calculate()
              {
                  path=new int[graph.length-1][];
                  int[] s=new int[graph.length];
                  for(int i : s)i=0; s[v]=2;
                  
                  //按路徑值從小到大的順序求解各條最短路徑

                  for(int i=0; i<graph.length-1; i++){
                      //求v到集合s2的最短路徑pointToSet[0]

                      int[][] pointToSet={{1, 1000, -1,},{1, 1000, -1,},};
                      for(int j=0; j<graph.length; j++){
                          if(s[j]==&& graph[v][j]<pointToSet[0][1]){
                              pointToSet[0][1]=graph[v][j];
                              pointToSet[0][0]=j;
                          }
                      }
                      
                      //求集合s1到集合s2的最短路徑setToSet[0]

                      int[][] setToSet={{1, 1000, -1,},};
                      for(int j=0; j<i; j++){
                          //求頂點(diǎn)path[j][0]到點(diǎn)集s2的最短路徑pointToSet[1]

                          pointToSet[1][1]=1000; pointToSet[1][2]=j;
                          for(int k=0; k<graph.length; k++){
                             if(s[k]==&& graph[path[j][0]][k]<pointToSet[1][1]){
                                  pointToSet[1][1]=graph[path[j][0]][k];
                                  pointToSet[1][0]=k;
                              }
                          }
                          pointToSet[1][1]=pointToSet[1][1]+path[j][1];
                          if(pointToSet[1][1]<setToSet[0][1]){
                              setToSet[0]=pointToSet[1];
                          }
                      }
                      
                      //比較pointToSet[0]及setToSet[0],求其小者,作為path[i]之值

                      if(pointToSet[0][1]<setToSet[0][1])
                          path[i]=pointToSet[0];
                      else
                          path[i]=setToSet[0];
                          
                      s[path[i][0]]=1; //把頂點(diǎn)劃為已求最短路終點(diǎn)之點(diǎn)集

                  }
              }

              int[][] getPath()
              {
                  return path;
              }
          }

           

          輸出結(jié)果:
          源點(diǎn):0; 終點(diǎn):2; 長(zhǎng)度:10; 終點(diǎn)前趨:-1
          源點(diǎn):0; 終點(diǎn):4; 長(zhǎng)度:30; 終點(diǎn)前趨:-1
          源點(diǎn):0; 終點(diǎn):3; 長(zhǎng)度:50; 終點(diǎn)前趨:1
          源點(diǎn):0; 終點(diǎn):5; 長(zhǎng)度:60; 終點(diǎn)前趨:2

          posted on 2009-04-15 22:19 weesun一米陽光 閱讀(782) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA源碼總結(jié)備用
          主站蜘蛛池模板: 乌兰察布市| 连山| 霞浦县| 界首市| 罗平县| 武夷山市| 三都| 民乐县| 青田县| 腾冲县| 榕江县| 民丰县| 临海市| 尉氏县| 政和县| 黄冈市| 南雄市| 凌云县| 湖北省| 垣曲县| 体育| 乡城县| 普陀区| 吉隆县| 大兴区| 城口县| 宣城市| 虎林市| 仁化县| 库伦旗| 寿光市| 芒康县| 北辰区| 武宁县| 湘潭市| 平安县| 黄陵县| 北京市| 新巴尔虎右旗| 西畴县| 通州市|