选择最佳线路(二)

如何要改进?

在上文中,讨论了最佳线路计算的数据结构的算法。但在交通线路的描述中,对网站线路孤立的起点,为了防止计算中无效循环,人为增加了下一结点为-1的虚拟点。但增加结点ID为-1的点,与现实不符。本文试图取消ID为-1的结点,并对算法进行修改。

修改后的深圳地铁的描述为

csharp 复制代码
string[] nsStr = [
     "1:1",
     "0:1>2:1>6:1>16:1",
     "1:4>3:6>7:3>15:1",
     "2:1>4:2>9:3>15:1",
     "3:2>15:1>10:3",
     "6:4",
     "1:6>5:3>7:5>11:1",
     "8:1>2:1>9:1",
     "7:1>9:1>12:1",
     "7:1>8:1>10:1>3:1" ,
     "4:1>9:1",
     "6:1>12:1",
     "8:1>11:1>13:1>14:1",
     "12:1",
     "12:1",
     "2:1>3:1>4:1>20:1>21:1",
     "1:1>18:1",
     "18:1",
     "16:1>17:1>19:1>20:1",
     "18:1>20:1",
     "18:1>19:1>15:1",
     "15:1"
     ];
      for (int i=0;i<nsStr.Length;i++)
   {
       ns = new NodeStation(i, nsStr[i]);
       lns.Add(ns); 
   }

算法修改为

csharp 复制代码
 public bool FindNodeWay(List<NodeStation> lns, List<NodeWay> listWay, int start, int end)
 {
     bool b = false, first, arrive = false;
     string s0, s1, s2;
     string[] sa, sb;
     int dist0 = 0, d1, nextStation;
     NodeStation ns;
     NodeWay nw, nw0;
     if (listWay.Count == 0)
     {
         ns = lns[start];
         sa = ns.Child.Split(">");
         for (int i = 0; i < sa.Length; i++)
         {
             sb = sa[i].Split(":");
             if (sb.Length > 1)
             {
                 nw = new NodeWay();
                 nw.Next = int.Parse(sb[0]);
                 nw.WayStr = $">{start}>{nw.Next}>";
                 nw.Distance = int.Parse(sb[1]);
                 listWay.Add(nw);
             }
         }
     }
   
     for (int i = 0; i < listWay.Count; i++)
     {              
         nw = listWay[i];
         s0 = nw.WayStr;
         dist0 = nw.Distance;
         first = true;
         if (!nw.Arrive)
         {
             ns = lns[nw.Next];
             sa = ns.Child.Split(">");
             for (int j = 0; j < sa.Length; j++)
             {
                 sb = sa[j].Split(":");
                 if (sb.Length > 1)
                 {
                     nextStation = int.Parse(sb[0]);
                     d1 = int.Parse(sb[1]);
                     s1 = $">{nextStation}>";
                     s2 = $"{nextStation}>";
                     if (!nw.WayStr.Contains(s1))
                     {
                         arrive = nextStation == end ? true : false;
                         if (first)
                         {
                             first = false;
                             nw.Arrive = arrive;
                             nw.Next = nextStation;
                             nw.Distance = dist0 + d1; ;
                             nw.WayStr = s0 + s2;
                         }
                         else
                         {
                             nw0 = new NodeWay();
                             nw0.WayStr = s0 + s2;
                             nw0.Next = nextStation;
                             nw0.Arrive = arrive;
                             nw0.Distance = dist0 + d1;
                             listWay.Add(nw0);
                         }
                     }
                 }
             }
             if (first)
             {
                 listWay.RemoveAt(i);
             }
             i = -1;
         }
     }
      return b;
 }

改进后的作用

按上述改进后,有两方面的提升

1.线路描述更加直观,简洁。

2.线路计算由于不再进行-1结点判断,效率提升。只要线路无新增结点,立即删除,减少剩余计算量。

相关推荐
liuyuzhongcc1 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
北_鱼1 小时前
支持向量机(SVM):算法讲解与原理推导
算法·机器学习·支持向量机
计算机小白一个2 小时前
蓝桥杯 Java B 组之背包问题、最长递增子序列(LIS)
java·数据结构·蓝桥杯
MZWeiei3 小时前
PTA:运用顺序表实现多项式相加
算法
卑微的小鬼3 小时前
数据库使用B+树的原因
数据结构·b树
GISer_Jing3 小时前
Javascript排序算法(冒泡排序、快速排序、选择排序、堆排序、插入排序、希尔排序)详解
javascript·算法·排序算法
cookies_s_s3 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
不想编程小谭3 小时前
力扣LeetCode: 2506 统计相似字符串对的数目
c++·算法·leetcode
水蓝烟雨4 小时前
[HOT 100] 2187. 完成旅途的最少时间
算法·hot 100
醉城夜风~4 小时前
[数据结构]双链表详解
数据结构