示例#1
0
        public void buildChain(char headLetter) //当没有规定headLetter时,传入#
        {
            for (int i = 0; i < size; i++)      //将每个点到源点的距离标为负无穷
            {
                distance[i] = MINI;
            }
            if (headLetter == '#')
            {
                for (char x = 'a'; x <= 'z'; x++)
                {
                    buildChain(x);
                }
            }
            else
            {
                topoList = new List <char>(topoListCopy.ToArray());
                distance[headLetter - 'a'] = 0;
            }

            while (topoList.Count != 0) //当前拓扑序列非空(在指定起点的情况下,是否需要把之前的全清空)
            {
                //取出拓扑序列的第一个点
                char u = topoList[0];
                topoList.RemoveAt(0);

                //更新所有邻接点的距离
                if (distance[u - 'a'] != MINI)
                {
                    for (int i = ReadFile.indexOfAllLetter[u - 'a']; i < ReadFile.endOfAllLeter[u - 'a']; i++)
                    {
                        Word w = ReadFile.wordList[i];

                        if (distance[w.Get_tail() - 'a'] < (distance[w.Get_head() - 'a'] + w.getWeight()))
                        {
                            if (WordChain.buildEnd(w.Get_head(), w.Get_tail())) //到达终点
                            {
                                //在到达终点的时候记录当前最长链和最长路长

                                //将拓扑序列清空,停止循环
                                topoList.Clear();
                                break;
                            }

                            distance[w.Get_tail() - 'a'] = (distance[w.Get_head() - 'a'] + w.getWeight()); //更新距离

                            //更新链
                            wordChainList[w.Get_tail() - 'a'].copyChain(wordChainList[w.Get_head() - 'a']);
                            wordChainList[w.Get_tail() - 'a'].addWord(w);
                        }
                    }
                }
            }
        }