dikesitela 迪克斯特拉算法

???????????????仅供参考 , 是否有bug还未进一步验证

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dikesitela
{
    class Dikesitela
    {
        //for save
        List<Dictionary<int, int>> list = new List<Dictionary<int, int>>();

        Dictionary<int, int> dict = new Dictionary<int, int>();

        int[,] routeValue = new int[7, 7];


        int[] routeCount;

        int k = 110;
        /// <summary>
        /// 比如0-1 ,可能有好几条线路可以到, 只有一条线路的未必是最短的
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="end"></param>
        public void Calcu(int origin, int end)
        {
            Dictionary<string, string> openWith =
    new Dictionary<string, string>();

            // Add some elements to the dictionary. There are no 
            // duplicate keys, but some of the values are duplicates.
            openWith.Add("txt", "notepad.exe");
            openWith.Add("bmp", "paint.exe");
            openWith.Add("dib", "paint.exe");
            openWith.Add("rtf", "wordpad.exe");

            string st = openWith["txt"];

            routeValue[1, 2] = 2;
            routeValue[1, 4] = 1;
            routeValue[1, 5] = 10;
            routeValue[1, 6] = k;
            routeValue[1, 3] = k;
            routeValue[2, 1] = k;
            routeValue[2, 3] = 2;
            routeValue[2, 4] = k;

            routeValue[2, 5] = k;
            routeValue[2, 6] = k;
            routeValue[3, 6] = 1;
            routeValue[3, 2] = k;
            routeValue[3, 1] = k;
            routeValue[3, 5] = k;
            routeValue[3, 4] = k;
            routeValue[4, 6] = 1;
            routeValue[4, 2] = k;
            routeValue[4, 1] = k;
            routeValue[4, 5] = k;
            routeValue[4, 3] = 1;
            routeValue[5, 6] = 11;
            routeValue[5, 2] = k;
            routeValue[5, 1] = k;
            routeValue[5, 3] = k;
            routeValue[5, 4] = k;
            //routeCount.Count = end - origin;
            for (int i = origin; i <= end; i++)
            {
                #region 第一次找线路,全都加到集合中  
                /*这步成功
                 * 
                 * */
                if (i == origin)
                {
                    for (int j = 1; j <= end; j++)
                    {
                        int temp = routeValue[i, j];
                        if (temp != 0) //等于0的不要加
                        {
                            dict.Add(j, temp);
                        }
                    }
                }
                #endregion
                else
                {
                    for (int j = 1; j <= end; j++)
                    {
                        int temp = routeValue[i, j];
                        if (temp != k && temp != 0)
                        {
                            int tep = dict[i];
                            if (tep != k && dict[j] != k && (tep + routeValue[i, j]) < dict[j] || (tep != k && dict[j] == k))
                            {
                                dict[j] = routeValue[i, j]+tep;
                            }
                            //else if ((tep != k && dict[j] == k))
                            //{
                            //    dict[j] = routeValue[i, j];

                            //}
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (i == origin)  //只是第一次加无穷
                            {
                                dict.Add(j, k);
                            }
                        }
                        //list.Add(dict); // 一次计算
                        // dict.Remove(j);
                    }
                }
            }
            for (int i = 0; i < dict.Count; i++)
            {
                Console.WriteLine(dict[i]);
            }

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值