static void Main(string[] args) { Program p = new Program(); p.Compute1(); Console.Read(); }
public void Compute1() { int max = 100;//我们认为最大就是100 int[] book = new int[6]; int[] dis = new int[6]; int[,] danYuan = new int[6, 6]{ {0,1,12,0,0,0}, {0,0,9,3,0,0}, {0,0,0,0,5,0}, {0,0,4,0,13,15}, {0,0,0,0,0,4}, {0,0,0,0,0,0} }; for (int i = 0; i < danYuan.GetLength(0); i++) { for (int j = 0; j < danYuan.GetLength(1); j++) { if (danYuan[i,j]==0) { danYuan[i, j] = max; } if (j==i) { danYuan[i, j] = 0; } } } for (int t = 0; t < 6; t++) { dis[t] = danYuan[0, t]; //读第一行数据 book[t] = 0; } book[0] = 1;//表示自己到自己已经走过了 int min,u=0; for (int i = 0; i < 6; i++) { min = max; //找到一到其他地点最短路径 for (int j = 0; j < 6; j++) { //如果这个点没有走过,并且这个点是没有走过中最小的,那么就将他的值赋给min,位置赋给u if (book[j]==0&&dis[j]<min) { min = dis[j]; u = j; } } //在这里u表示的是第几行 book[u] = 1;//设置到1最近顶点已经确认了 for (int e = 0; e < 6; e++) { if (danYuan[u,e]<max)//判断是否可行 { //判断当前是否是最短的 if (dis[e]>danYuan[u,e]+dis[u]) { dis[e] = danYuan[u, e] + dis[u]; } } } } for (int ew = 0; ew < 6; ew++) { Console.WriteLine(dis[ew]); } }
时间: 2024-12-22 16:59:36