c++
来源:互联网 发布:艾默生网络能源 待遇 编辑:程序博客网 时间:2024/06/13 23:12
简洁
/*Program to find shortest path between two hot-points in the map*/namespace Process {public class DijkSingle {private static int MAX_SIZE = StaticVar.NodeCount;private Double[, ] a = new Double[MAX_SIZE + 1, MAX_SIZE + 1]; //weight pairs for each edgestatic int v = 1; // index of starting hot-pointprivate Double[] dist = new Double[MAX_SIZE + 1]; //store the distance between starting hot-point and each other hot-pointprivate int[] prev = new int[MAX_SIZE + 1];public DijkSingle(ArrayList lockedways) {InitData(); // get the map data from dbfor (int i = 0; i < lockedways.Count; i++) { // initialize distances of all vertices as infinitestring val = lockedways[i] + "";int startPoint = int.Parse(val.Split('-')[0]);int endPoint = int.Parse(val.Split('-')[1]);a[startPoint, endPoint] = Double.MaxValue;a[endPoint, startPoint] = Double.MaxValue;}}private void Dijkstra(int v, Double[, ] a, Double[] dist, int[] prev) {int n = dist.Length - 1;if (v < 1 || v > n) return;bool[] s = new bool[n + 1]; //set a mark to each hot-point whether it has already been vistedfor (int i = 1; i <= n; i++) {dist[i] = a[v, i];s[i] = false;if (dist[i] == Double.MaxValue) prev[i] = 0;else prev[i] = v;}dist[v] = 0;s[v] = true; //assum that starting hot-point has been visitedfor (int i = 1; i < n; i++) {Double temp = Double.MaxValue;int u = v;for (int j = 1; j <= n; j++) //get current shortest pathif ((!s[j]) && (dist[j] < temp)) {u = j;temp = dist[j];}s[u] = true;for (int j = 1; j <= n; j++)if ((!s[j]) && (a[u, j] < Double.MaxValue)) {Double newdist = dist[u] + a[u, j];if (newdist < dist[j]) {dist[j] = newdist;prev[j] = u;}}}}//check the starting hot-point in the road with obstacle whether or not exist in the Vist Pathpublic bool isInpath(ArrayList lockedways, ArrayList visitPath) {bool isInpath = false;for (int i = 0; i < lockedways.Count; i++) {string val = lockedways[i] + "";int startPoint = int.Parse(val.Split('-')[0]);int endPoint = int.Parse(val.Split('-')[1]);if (visitPath.Contains(startPoint) && visitPath.Contains(endPoint)) isInpath = true;}return isInpath;}//get the shortest path between starting hot-point and ending hot-pointpublic ArrayList getSingleDijk(int startPoint, int endPoint) {ArrayList res = new ArrayList();v = startPoint;Dijkstra(v, a, dist, prev);res.Add(endPoint);if (dist[endPoint] < Double.MaxValue) {res.Add(prev[endPoint]);while (prev[endPoint] != startPoint) {res.Add(prev[prev[endPoint]]);endPoint = prev[endPoint];}res.Reverse();return res;}else return res;}public void InitData() {// get the hot-points from the db// do something ...}}}
阅读全文
0 0
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- Eric6与pyqt5学习笔记 6【实战2 window伪文本编辑器】【纯eric6操作】
- 【剑指offer】二叉搜索树的后续遍历
- [LC][Array] 448. Find All Numbers Disappeared in an Array
- CRB and His Birthday
- 水仙花数改进版
- c++
- angularjs中常见的工具方法
- Get 404 by @RequestBody
- web中用c#设计一个简易计算器
- java 获取路径的各种方法
- 【shell】sed
- 根路径
- Date、LocalTime、LocalDate、LocalDate-时间操作工具类
- NYACM_001