Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. void dij( int rootref, string endCity, string endSt, graf& g)
  2. {
  3. intersections * root = g.interArr[rootref]; // interArr is an array of intersection nodes
  4. Q * kyu = new Q(20000); // create priority queue (min heap)
  5. double totalDist = 0; // initialize total distance to zero
  6. kyu->insert(root); // insert root into priority queue
  7.  
  8. while(!kyu->isEmpty()) // while queue is not empty
  9. {
  10. intersections * popped = kyu -> extractMin(); // extract top of queue
  11. popped->visited = 1; // mark as visited
  12. if(popped->nearestCity == endCity && popped->state==endSt)
  13. break;
  14. int connSize = popped->c; // get size of array containing refference
  15.  
  16. for(int i = 0; i<connSize;i++) // points to connecting intersections
  17. {
  18. int reff = popped->adjacents[i]->B; // get refference for adjacent vertex
  19. double d = popped->adjacents[i]->distance; // get distance from popped node to adjacent vertex
  20. if(g.interArr[reff]->visited==0) // if adjacent vertex has not been visited
  21. {
  22. double alt = d + totalDist; // measure distance from root to this vertex
  23. if(alt<g.interArr[reff]->dijDist) // if this distance is less than dijDist (initialized very high)
  24. {
  25. g.interArr[reff]->dijDist = alt; // update dijDist distance
  26. totalDist = alt; // update total distance
  27. kyu->insert(g.interArr[reff]); // insert into priority queue
  28. }
  29.  
  30. }
  31. }
  32. }
  33. }
  34.  
  35. struct graf{
  36. intersections ** interArr;
  37. graf(){}
  38. void make()
  39. {
  40. interArr = new intersections*[29146];
  41. }
  42.  
  43. void insert(double la, double lo, double dist, string na,string st, int entry)
  44. {
  45. interArr[entry] = new intersections(la,lo,dist,na,st);
  46. interArr[entry]->visited = 0; interArr[entry]->dijDist = 10000000;
  47. }
  48. }
  49.  
  50. struct intersections{
  51. double lat, lon, distanceToNearestPlace;
  52. string nearestCity, state;
  53. connections ** adjacents;
  54. double c, dijDist, visited;
  55. intersections * next, *prev;
  56.  
  57. intersections(){
  58. c = 0;
  59. next = NULL; dijDist = 10000000; visited = 0; prev =NULL;
  60. adjacents = new connections*[24];
  61. for(int i = 0; i<24; i++)
  62. adjacents[i] = new connections;
  63. }
  64. intersections(double la, double lo, double dist, string nc, string st)
  65. { lat = la; lon = lo; distanceToNearestPlace = dist; nearestCity = nc; state = st;
  66. c = 0; dijDist = 10000000; visited = 0;
  67. next = NULL; prev = NULL;}
  68.  
  69. void addToAdjacents(int a1, int dontuse, double mil, string rd, string t){
  70. if(c==0){
  71. adjacents = new connections*[24];
  72. for(int i = 0; i<24; i++)
  73. adjacents[i] = new connections;
  74. }
  75. int d = c;
  76. adjacents[d] = new connections(a1,dontuse,mil,rd,t);
  77. dijDist = 10000000; visited = 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement