/// <summary> /// 计算从点s到其他所有点的路径 /// </summary> /// <param name="s"></param> /// <returns></returns> public List <int> CalculatePath(int s) { List <bool> visited = new List <bool>(polys.Count); List <float> length = new List <float>(polys.Count); List <int> from = new List <int>(polys.Count); for (int i = 0; i < polys.Count; i++) { visited.Add(false); length.Add(Single.MaxValue); from.Add(-1); } RBTree <float, int> queue = new RBTree <float, int>(); visited[s] = true; length[s] = 0f; queue.Add(0f, s); while (queue.Count > 0) { KeyValuePair <float, int> vPair = queue.Min(); int v = vPair.Value; queue.Remove(vPair.Key); //每次拿出来的最小值一定属于最短路径(贪心性质) visited[v] = true; if (NearPolys.ContainsKey(v)) { foreach (PolyConnection e in nearPolys[v]) { int w = e.Other(v); if (!visited[w]) { float l = length[v] + e.Weight; if (from[w] == -1 || l < length[w]) { length[w] = l; from[w] = v; if (queue.ContainsValue(w)) {//这里用sortedList的更新操作(删除再插入)来实现优先队列的更新 queue.Remove(queue.KeyOfValue(w)); queue.Add(l, w); } else { queue.Add(l, w); } } } } } } return(from); }