???????????????仅供参考 , 是否有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]);
}
}
}
}
dikesitela 迪克斯特拉算法
最新推荐文章于 2024-08-19 12:02:02 发布