Advertisement
Guest User

Prim's sub graph

a guest
Jul 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class List
  4. {
  5. private:
  6.     int* list;
  7.     int reserved;
  8.     int size;
  9.     int increase = 20;
  10.     void resize()
  11.     {
  12.         int* temp = new int[reserved + increase];
  13.         for (int i=0; i < size; i++)
  14.             temp[i] = list[i];
  15.         delete [] list;
  16.         list = temp;
  17.         reserved += increase;
  18.     }
  19. public:
  20.     List()
  21.     {
  22.         reserved = 10;
  23.         size = 0;
  24.         list = new int[reserved];
  25.     }
  26.     void add(int element)
  27.     {
  28.         if (size < reserved - 5)
  29.             resize();
  30.         list[size] = element;
  31.         size++;
  32.     }
  33.     int getSize()
  34.     {
  35.         return size;
  36.     }
  37.     int getElement(int n)
  38.     {
  39.         return list[n];
  40.     }
  41.     void changeElement(int index, int value)
  42.     {
  43.         list[index] = value;
  44.     }
  45.     void show()
  46.     {
  47.         cout << endl;
  48.         cout << "[ ";
  49.         for (int i=0; i<size; i++)
  50.             cout << list[i] << " ";
  51.         cout <<"]"<< endl;
  52.     }
  53.     int isIn(int element)
  54.     {
  55.         for (int i=0; i< size; i++)
  56.         {
  57.             if (list[i] == element)
  58.             {
  59.                 return i;
  60.             }
  61.         }
  62.         return -1;
  63.     }
  64. };
  65.  
  66. struct node
  67. {
  68.     int number;
  69.     List connected;
  70.     List costs;
  71. };
  72.  
  73.  
  74. int sub(node* nodes, int init, int numnodes)
  75. {
  76.     List visited;
  77.     visited.add(init);
  78.     int numKids;
  79.     int size;
  80.     int cost;
  81.     int newCost;
  82.     int source;
  83.     int dist;
  84.     int cheapest;
  85.     bool flag = false;
  86.     int huge = 9999999;
  87.     int solution = 0;
  88.     int** matrix = new int*[numnodes];
  89.     int mySource;
  90.     for (int i=0; i< numnodes; i++)
  91.     {
  92.         matrix[i] = new int[numnodes];
  93.         for (int j=0; j < numnodes; j++)
  94.         {
  95.             matrix[i][j] = 0;
  96.         }
  97.     }
  98.     while (visited.getSize() != numnodes)
  99.     {
  100.         size = visited.getSize();
  101.         cost = huge;
  102.         cheapest = -1;
  103.         mySource = -1;
  104.         for (int v=0; v<size; v++)
  105.         {
  106.             source = visited.getElement(v);
  107.             numKids = nodes[source].connected.getSize();
  108.             for (int kid=0; kid < numKids; kid++)
  109.             {
  110.                 dist = nodes[source].connected.getElement(kid);
  111.                 if (matrix[source][dist] == -1)
  112.                     continue;
  113.                 newCost = nodes[source].costs.getElement(kid);
  114.                 if (newCost < cost && matrix[source][dist] != -1&&visited.isIn(dist)==-1)
  115.                 {
  116.                     cost = newCost;
  117.                     mySource = source;
  118.                     cheapest = dist;
  119.                 }
  120.             }
  121.         }
  122.        
  123.         if (cost != huge)
  124.         {
  125.             //cout << "source: " << mySource << " destination: " << cheapest << " cost " << cost << endl;
  126.             solution += cost;
  127.             if (visited.isIn(cheapest)==-1)
  128.                 visited.add(cheapest);
  129.         }
  130.         matrix[mySource][cheapest] = -1;
  131.         matrix[cheapest][mySource] = -1;
  132.  
  133.     }
  134.     return solution;
  135. }
  136.  
  137.  
  138. int main() {
  139.     /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  140.     int nodes;
  141.     int edges;
  142.     cin >> nodes;
  143.     cin >> edges;
  144.     node* node_list = new node[nodes];
  145.     for (int i=0; i < nodes; i++)
  146.         node_list[i].number = i;
  147.     int first;
  148.     int second;
  149.     int third;
  150.     int initial;
  151.     for (int i=0; i<edges; i++)
  152.     {
  153.         cin >> first;
  154.         cin >> second;
  155.         first --;
  156.         second --;
  157.         cin >> third;
  158.         if (node_list[first].connected.isIn(second)==-1)
  159.         {
  160.             node_list[first].connected.add(second);
  161.             node_list[first].costs.add(third);
  162.         }
  163.         else if (node_list[first].costs.getElement(node_list[first].connected.isIn(second)) > third)
  164.         {
  165.             node_list[first].costs.changeElement(node_list[first].connected.isIn(second),third);
  166.         }
  167.         if (node_list[second].connected.isIn(first)==-1)
  168.         {
  169.             node_list[second].connected.add(first);
  170.             node_list[second].costs.add(third);
  171.         }
  172.         else if (node_list[second].costs.getElement(node_list[second].connected.isIn(first)) > third)
  173.         {
  174.             node_list[second].costs.changeElement(node_list[second].connected.isIn(first),third);
  175.         }      
  176.     }
  177.     cin >> initial;
  178.     initial --;
  179.     int sol;
  180.     sol = sub(node_list, initial, nodes);
  181.     cout << sol << endl;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement