我需要读取一个包含矩阵的文本文件,而不是像我一样将它放在代码中,但我尝试过但我无法弄清楚。我在文本文件上有矩阵,就像我在代码上有的一样,但没有昏迷这是一个dijkstra程序,它读取矩阵以找到最短路径,谢谢{ private static int MinimumDistance(int[] distance, bool[] shortestPathTreeSet, int verticesCount) { int min = int.MaxValue; int minIndex = 0; for (int v = 0; v < verticesCount; ++v) { if (shortestPathTreeSet[v] == false && distance[v] <= min) { min = distance[v]; minIndex = v; } } return minIndex; } private static void Print(int[] distance, int verticesCount) { Console.WriteLine("Vertex Distance from source"); for (int i = 0; i < verticesCount; ++i) Console.WriteLine("{0}\t {1}", i, distance[i]); } public static void DijkstraAlgo(int[,] graph, int source, int verticesCount) { int[] distance = new int[verticesCount]; bool[] shortestPathTreeSet = new bool[verticesCount]; for (int i = 0; i < verticesCount; ++i) { distance[i] = int.MaxValue; shortestPathTreeSet[i] = false; } distance[source] = 0; for (int count = 0; count < verticesCount - 1; ++count) { int u = MinimumDistance(distance, shortestPathTreeSet, verticesCount); shortestPathTreeSet[u] = true; for (int v = 0; v < verticesCount; ++v) if (!shortestPathTreeSet[v] && Convert.ToBoolean(graph[u, v]) && distance[u] != int.MaxValue && distance[u] + graph[u, v] < distance[v]) distance[v] = distance[u] + graph[u, v]; } Print(distance, verticesCount); }这是文本文件矩阵:0 6 8 12 0 0 0 0 0 06 0 0 5 0 0 0 0 0 08 0 0 1 0 0 0 0 0 012 5 1 0 9 10 14 16 15 00 0 0 3 0 0 00 0 0 10 0 0 0 0 13 00 0 0 14 3 0 0 3 0 60 0 0 16 0 0 3 0 1 40 0 0 15 0 13 0 1 0 70 0 0 0 0 4 0
1 回答
- 1 回答
- 0 关注
- 266 浏览
添加回答
举报
0/150
提交
取消