C#城市线路图的纯算法以及附带求极权值

常用的数据结构写出来 纯属于算法 性方面还有待提高 时间复杂度最坏情况下O(2^n)  最优:O(n^2)

线路图为双向 带有权值  比如A-B距离是5000km 那么B-A有可能不是5000km 所以我在LoadData方法时候没做交换变量直接存放在集合里面

以起点递归查找下一连接点并返回当作起点节点查找      代码虽然有些乱 本想调整 !


  static List maindata = null;
        static int isend = 1;
        static List fresult = new List();

        static void Main(string[] args)
        {
            string begin = "重庆";
            string end = "厦门";
            LoadData();
            Program pl = new Program();
            List beginlist = new List();
            beginlist.Add(begin);
            pl.GetF(beginlist);

            foreach (string a in fresult)
                Console.WriteLine(a);
            Console.WriteLine(fresult.Count);
            //main data end

            List searchlist = new List();
            string temp = "";
            foreach (string f in fresult)
            {
                if (f.IndexOf(end) > -1)
                {
                    temp = f.Substring(0, f.LastIndexOf(end) + end.Length);
                    if (searchlist.Contains(temp) == false)
                        searchlist.Add(temp);
                }
            }
            Console.WriteLine(begin + "------------->" + end + ":");
            foreach (string a in searchlist)
                Console.WriteLine(a);
            Console.WriteLine(searchlist.Count);
            //search data   A to B

            string a1 = "权最大为:" + GetMaxQuk(searchlist);
            Console.WriteLine(a1);
            a1 = "权最小为:" + GetMinQuk(searchlist);
            Console.WriteLine(a1);

            Console.ReadKey();
        }

  取最大的权值数据
        private static string GetMaxQuk(List nage) 
        {
            string resultsrt = "";

            string[] nagearry = null;
            int val, maxval = 0;
            for (int s = 0; s < nage.Count; s++)
            {
                nagearry = nage[s].Split('-');//s个数组
                val = GetVal(nagearry);
                if (val > maxval)
                {
                    maxval = val;
                    resultsrt = nage[s] + ":" + val;
                }
                nagearry = null;
            }
            return resultsrt;
        }

取最小的权值数据
        private static string GetMinQuk(List nage)
        {
            string resultsrt = "";
            string[] nagearry = null;
            int val, minval = int.MaxValue;
            for (int s = 0; s < nage.Count; s++)
            {
                nagearry = nage[s].Split('-');//s个数组
                val = GetVal(nagearry);
                if (val < minval)
                {
                    minval = val;
                    resultsrt = nage[s] + ":" + val;
                }
                nagearry = null;
            }
            return resultsrt;
        }

  具体取权值的方法
        private static int GetVal(string[] findarry)
        {
            int val = 0;
            for (int ss = 0; ss < findarry.Length - 1; ss = ss + 1)
            {
                foreach (string[] aa in maindata)
                {
                    if (aa[0] == findarry[ss] && aa[1] == findarry[ss + 1])
                    {
                        val += Convert.ToInt32(aa[2]);
                        break;
                    }
                }
            }
            return val;
        }

        List GetF(List beginlist)
        {
           //此处省略几十行代码 需要完整代码请联系an
            if (isend == 0)
                return GetF(returnlist);
            else
                return null;
        }

加载绑定数据
       static void LoadData()
       {
            List backlist = null;
            string[] arry = null;

            backlist = new List();

            arry = new string[3];
            arry[0] = "重庆";
            arry[1] = "北京";
            arry[2] = "3000";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "重庆";
            arry[1] = "广州";
            arry[2] = "2500";
            backlist.Add(arry);
            arry = null;

            arry = new string[3];
            arry[0] = "北京";
            arry[1] = "重庆";
            arry[2] = "3000";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "北京";
            arry[1] = "广州";
            arry[2] = "3100";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "北京";
            arry[1] = "长沙";
            arry[2] = "2800";
            backlist.Add(arry);
            arry = null;

            arry = new string[3];
            arry[0] = "长沙";
            arry[1] = "北京";
            arry[2] = "2800";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "长沙";
            arry[1] = "广州";
            arry[2] = "1500";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "长沙";
            arry[1] = "厦门";
            arry[2] = "800";
            backlist.Add(arry);
            arry = null;

            arry = new string[3];
            arry[0] = "广州";
            arry[1] = "重庆";
            arry[2] = "2500";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "广州";
            arry[1] = "北京";
            arry[2] = "3100";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "广州";
            arry[1] = "长沙";
            arry[2] = "1500";
            backlist.Add(arry);
            arry = null;
            maindata = backlist;

 
            arry = new string[3];
            arry[0] = "厦门";
            arry[1] = "长沙";
            arry[2] = "800";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "厦门";
            arry[1] = "广州";
            arry[2] = "500";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "广州";
            arry[1] = "厦门";
            arry[2] = "500";
            backlist.Add(arry);
            arry = null;

 
            arry = new string[3];
            arry[0] = "广州";
            arry[1] = "云南";
            arry[2] = "3200";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "云南";
            arry[1] = "广州";
            arry[2] = "3200";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "云南";
            arry[1] = "长沙";
            arry[2] = "3500";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "长沙";
            arry[1] = "云南";
            arry[2] = "3500";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "云南";
            arry[1] = "厦门";
            arry[2] = "5400";
            backlist.Add(arry);
            arry = null;
            arry = new string[3];
            arry[0] = "厦门";
            arry[1] = "云南";
            arry[2] = "5400";
            backlist.Add(arry);
            arry = null;

        }
 
完整代码信息异步同城信息港

你可能感兴趣的:(windows)