sunfruit[請訪問http://www.fruitres.cn]

          --我相信JAVA能走得更遠 QQ:316228067

          導航

          <2006年10月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          統計

          公告

          個人寫的作品會盡量附上代碼,大家使用發現問題就指出,交流第一嘛  QQ:316228067

          常用鏈接

          留言簿(13)

          隨筆分類(121)

          隨筆檔案(105)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          [原創]圖論應用--最短路徑

              --sunfruit

          求上圖1點到其他各點的最短路徑,依據圖論知識建立矩陣模型,進一步得到代碼如下

          public class ShortPathA {

            private static int[][]
                a = {
                {0, 50, 10, 100000, 45, 100000}, {100000, 0, 15, 100000, 10, 100000}, {20, 100000, 0, 15, 100000, 100000}, {
                100000, 20, 100000, 0, 35, 100000}, {100000, 100000, 1000000, 30, 0, 100000}, {100000, 100000, 100000, 3, 100000, 0}
            };

            private static boolean[] mark = new boolean[a.length];
            public ShortPathA() {
              int Vo = 0; //源點
              //源點到其他各點的距離
              int[] b = new int[a.length];
              DynArrayInt S = new DynArrayInt();
              for (int i = 0; i < a.length; i++) {
                mark[i] = false;
                //b[i] = a[Vo][i];
              }
              int best = -1;
              mark[0] = true;
              b[0] = 0; //{0為源點}
              while (best != 0) {
                best = 0;
                int best_j = 0;
                for (int i = 0; i < b.length; i++)
                {
                  if (mark[i]) //{對每一個已計算出最短路徑的點}
                  {
                    for (int j = 0; j < b.length; j++) {
                      if ( (!mark[j]) && (a[i][j] > 0)) {
                        if ( (best == 0) || (b[i] + a[i][j] < best)) {
                          best = b[i] + a[i][j];
                          best_j = j;
                        }
                      }
                    }
                  }
                }
                if (best > 0) {
                  b[best_j] = best;
                  mark[best_j] = true;
                }

              }
              System.out.println(java.util.Arrays.toString(b));
            }

            public static void main(String[] args) {
              ShortPathA shortpath = new ShortPathA();
            }

          }

          posted on 2006-10-23 21:17 sunfruit 閱讀(1698) 評論(0)  編輯  收藏 所屬分類: 數據結構


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 岑巩县| 日照市| 阳信县| 新宁县| 遂川县| 石河子市| 深泽县| 惠来县| 宁远县| 稷山县| 确山县| 简阳市| 德州市| 舟曲县| 石泉县| 镇坪县| 色达县| 米易县| 尼玛县| 义马市| 河源市| 常宁市| 海兴县| 林周县| 顺昌县| 岢岚县| 科尔| 阿克陶县| 阿合奇县| 双江| 广宗县| 砚山县| 牡丹江市| 沁水县| 太白县| 航空| 南安市| 云浮市| 礼泉县| 临潭县| 和田市|