Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <limits.h>
  5. #include <limits.h>
  6. using namespace std;
  7.  
  8. struct neighbour
  9. {
  10.     int numberofCity;
  11.     int distance;
  12. };
  13. class Node
  14. {
  15. private:
  16.     string name;
  17.     int countOfNeighbours;
  18.     neighbour *neightbours;
  19. public:
  20.     void getinput()
  21.     {
  22.        
  23.         cin >> name;
  24.    
  25.         cin >> countOfNeighbours;
  26.         neightbours = new neighbour[countOfNeighbours];
  27.         for (int i = 0; i < countOfNeighbours; i++)
  28.         {
  29.             cin >> neightbours[i].numberofCity;
  30.             neightbours[i].numberofCity -= 1;
  31.             cin >> neightbours[i].distance;
  32.         }
  33.     }
  34.     void deleteNode()
  35.     {
  36.         delete neightbours;
  37.     }
  38.     string returnName() { return name; }
  39.     neighbour returnNeighbours(int i){ return neightbours[i]; }
  40.     int returncountOfNeighbours() { return countOfNeighbours; }
  41.    
  42.  
  43. };
  44. class Graph
  45. {
  46. private:
  47.     int countofCities;
  48.     Node *cities;
  49. public:
  50.     void SetNodes()
  51.     {
  52.        
  53.         cin >> countofCities;
  54.         cities = new Node[countofCities];
  55.         for (int i = 0; i < countofCities; i++)
  56.         {
  57.             cities[i].getinput();
  58.         }
  59.     }
  60.     int returnIDofCity(string searchName)
  61.     {
  62.         int l;
  63.         for (int i = 0; i < countofCities; i++)
  64.         {
  65.             if (cities[i].returnName() == searchName)
  66.                 l = i;
  67.         }
  68.         return l;
  69.     }
  70.     int ReturnNumberOfCities() { return countofCities; }
  71.     Node getNode(int i) { return cities[i]; }
  72.  
  73.     void del()
  74.     {
  75.         for (int i = 0; i < countofCities; i++)
  76.             {
  77.                 cities[i].deleteNode();
  78.             }
  79.        
  80.     }
  81. };
  82. struct heapNode
  83. {
  84.     private:
  85.         int number;
  86.         int costs;
  87.     public:
  88.         void SetNode(int num, int cos)
  89.         {
  90.             number = num;
  91.             costs = cos;
  92.         }
  93.         int getCost() { return costs; }
  94.         int getNumber() { return number; }
  95. };
  96. class Heap
  97. {
  98. private:
  99.     int heap_size;
  100.     int capacity;
  101.     heapNode *heaparray;
  102. public:
  103.     Heap(int cap)
  104.     {
  105.         heap_size = 0;
  106.         capacity = cap;
  107.         heaparray = new heapNode[capacity];
  108.     }
  109.     int parent(int i) { return (i - 1) / 2; }
  110.     int left(int i) { return (2 * i + 1); }
  111.     int right(int i) { return (2 * i + 2); }
  112.     void swapNodes(heapNode *a, heapNode *b)
  113.     {
  114.         heapNode *tmp = new heapNode();
  115.         *tmp = *a;
  116.         *a = *b;
  117.         *b = *tmp;
  118.     }
  119.     void insert(heapNode *node)
  120.     {
  121.         if (heap_size == capacity)
  122.             return;
  123.  
  124.         heap_size++;
  125.         int i = heap_size - 1;
  126.         heaparray[i] = *node;
  127.  
  128.         while (i != 0 && heaparray[parent(i)].getCost() > heaparray[i].getCost())
  129.         {
  130.             swapNodes(&heaparray[parent(i)], &heaparray[i]);
  131.             i = parent(i);
  132.         }
  133.     }
  134.     heapNode getMin()
  135.     {
  136.         heapNode *tmp = new heapNode();
  137.         *tmp = heaparray[0];
  138.         if (heap_size == 1)
  139.         {
  140.             heaparray[heap_size - 1].SetNode(INT_MIN, INT_MIN);
  141.             heap_size--;
  142.         }
  143.         if (heap_size > 1)
  144.         {
  145.             swapNodes(&heaparray[0], &heaparray[heap_size - 1]);
  146.             heaparray[heap_size - 1].SetNode(INT_MIN, INT_MIN);
  147.             heap_size--;
  148.             repairHeap(0);
  149.         }
  150.        
  151.         return *tmp;
  152.     }
  153.  
  154.     void repairHeap(int i)
  155.     {
  156.         int l = i;
  157.         int left = 2 * i + 1;
  158.         int right = 2 * i + 2;
  159.  
  160.         if (left < heap_size && heaparray[left].getCost() < heaparray[i].getCost())
  161.             l = left;
  162.         if (right < heap_size && heaparray[right].getCost() < heaparray[l].getCost())
  163.             l = right;
  164.         if (l != i)
  165.         {
  166.             swapNodes(&heaparray[i], &heaparray[l]);
  167.             repairHeap(l);
  168.         }
  169.  
  170.     }
  171.     void printallnodes()
  172.     {
  173.         for (int i = 0; i < heap_size; i++)
  174.         {
  175.             cout << heaparray[i].getNumber() << " <- Miasto " << heaparray[i].getCost() << endl;
  176.         }
  177.     }
  178.     bool isEmpty()
  179.     {
  180.         if (heap_size > 0)
  181.         {
  182.             return false;
  183.         }
  184.         else return true;
  185.     }
  186.     void del()
  187.     {
  188.         delete heaparray;
  189.     }
  190.  
  191. };
  192. void Dijkstra(string startNode, string stopNode, Graph *graph)
  193. {
  194.     int nodesCount = graph->ReturnNumberOfCities();
  195.     int startPoint = graph->returnIDofCity(startNode);
  196.     int stopPoint = graph->returnIDofCity(stopNode);
  197.     bool *isVisited = new bool[nodesCount];
  198.     int *distance = new int [nodesCount];
  199.     int *fromNode = new int [nodesCount];
  200.     int currentNode;
  201.     Heap *heap = new Heap(nodesCount);
  202.     heapNode *tmp;
  203.     for (int i = 0; i < nodesCount; i++)
  204.     {
  205.         isVisited[i] = false;
  206.         distance[i] = INT_MAX;
  207.         fromNode[i] = -1;
  208.     }
  209.     currentNode=  startPoint ;
  210.     distance[currentNode] = 0;
  211.     isVisited[currentNode] = true;
  212.     heapNode *node = new heapNode();
  213.     node->SetNode(startPoint, 0);
  214.     heap->insert(node);
  215.     while (!heap->isEmpty())
  216.     {
  217.         heapNode curr = heap->getMin();
  218.         currentNode = curr.getNumber();
  219.         isVisited[currentNode] = true;
  220.         Node city = graph->getNode(currentNode);
  221.         int numOfNeigh = city.returncountOfNeighbours();
  222.         for (int i = 0; i < numOfNeigh; i++)
  223.         {
  224.             neighbour somsiad = city.returnNeighbours(i);
  225.            
  226.             if (!isVisited[somsiad.numberofCity])
  227.             {
  228.                 node->SetNode(somsiad.numberofCity, somsiad.distance + distance[currentNode]);
  229.                 heap->insert(node);
  230.             }
  231.             if (distance[somsiad.numberofCity] > somsiad.distance + distance[currentNode])
  232.             {
  233.                 distance[somsiad.numberofCity] = somsiad.distance + distance[currentNode];
  234.                 fromNode[currentNode];
  235.             }
  236.            
  237.         }
  238.     }
  239.     cout << distance[stopPoint];
  240.     heap->del();
  241.     delete heap;
  242. }
  243.  
  244. int main()
  245. {
  246.  
  247.     int testcount, pathToFind;
  248.     string startNode, stopNode;
  249.     cin >> testcount;
  250.     int i = 0;
  251.     while (i < testcount)
  252.     {
  253.         Graph *graph = new Graph();
  254.         graph->SetNodes();
  255.         //graph->printGraph();
  256.         cin >> pathToFind;
  257.         for (int j = 0; j < pathToFind; j++)
  258.         {
  259.             cin >> startNode >> stopNode;
  260.             Dijkstra(startNode, stopNode, graph);
  261.             cout << endl;
  262.         }
  263.         graph->del();
  264.         delete graph;
  265.         i++;
  266.     }
  267.    
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement