ForeverStrong

Untitled

Jan 30th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // AlgorytmDijkstry.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <string>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <Windows.h>
  9. #define const int MAXINT = 2147483647;
  10.  
  11.  
  12. using namespace std;
  13. struct slistEl
  14. {
  15.     slistEl * next;
  16.     int start, weight;
  17. };
  18.  
  19.  
  20.  
  21. void changecolor(int number_color) // Funkcja do zmienia koloru czcionki w consoli. Function to changing color of font in comand window.
  22. {
  23.     HANDLE handle;
  24.     handle = GetStdHandle(STD_OUTPUT_HANDLE);
  25.     SetConsoleTextAttribute(handle, number_color);
  26. }
  27.  
  28. int main()
  29. {
  30.     int i, j, howEdge,nodes, start, u, weight, index, edge, stos_index, *cost, *visited, *calculeted;
  31.     bool *QS;
  32.     slistEl **graf;
  33.     slistEl *sasiad, *backup;
  34.     ifstream plik;
  35.     plik.open("graph.txt");
  36.     plik >> start >> nodes >> howEdge;
  37.     cost = new int[nodes];
  38.     visited = new int[nodes];
  39.     QS = new bool[nodes];
  40.     graf = new slistEl *[nodes];
  41.     calculeted = new int[nodes];
  42.     stos_index = 0;
  43.     for (i = 0; i < nodes; i++)
  44.     {
  45.         cost[i] = MAXINT;
  46.         visited[i] = -1;
  47.         QS[i] = false;
  48.         graf[i] = NULL;
  49.     }
  50.     for (i = 0; i < howEdge; i++)
  51.     {
  52.         plik >> index >> edge >> weight;
  53.         sasiad = new slistEl;
  54.         sasiad->start = edge;
  55.         sasiad->weight = weight;
  56.         sasiad->next = graf[index];
  57.         graf[index] = sasiad;
  58.     }
  59.     cost[start] = 0;
  60.     for (i = 0; i < nodes; i++)
  61.     {
  62.  
  63.         for (j = 0; QS[j]; j++);
  64.         for (u = j++; j < nodes; j++)
  65.             if (!QS[j] && (cost[j] < cost[u])) u = j;
  66.         QS[u] = true;
  67.         for (sasiad = graf[u]; sasiad; sasiad = sasiad->next)
  68.             if (!QS[sasiad->start] && (cost[sasiad->start] > cost[u] + sasiad->weight))
  69.             {
  70.                 cost[sasiad->start] = cost[u] + sasiad->weight;
  71.                 visited[sasiad->start] = u;
  72.             }
  73.     }
  74.     cout << "Wierzcholek startowy to: ";
  75.     changecolor(4);
  76.     cout << start << endl;;
  77.     changecolor(7);
  78.     for (i = 0; i < nodes; i++)
  79.     {
  80.         if (i == start);
  81.         else {
  82.            
  83.             cout << " Droga do wierzcholka ";
  84.             changecolor(4);
  85.             cout << i;
  86.             changecolor(7);
  87.             cout << " wiedzie przez wierzcholki: ";
  88.             changecolor(4);
  89.             for (j = i; j > -1; j = visited[j]) calculeted[stos_index++] = j;
  90.             while (stos_index) cout << calculeted[--stos_index] << " ";
  91.             changecolor(7);
  92.             if (cost[i] >= MAXINT - 200 || cost[i] <= -MAXINT - 200) {
  93.                 string buff = "NIE ZNALEZIONO DROGI";
  94.                 cout << "i kosztuje: " << buff << " $ " << endl;
  95.             }
  96.             else cout << "i kosztuje: " << cost[i] << " $ " << endl;
  97.            
  98.         }
  99.     }
  100.     plik.close();
  101.     delete[] cost;
  102.     delete[] visited;
  103.     delete[] QS;
  104.     delete[] calculeted;
  105.  
  106.     for (i = 0; i < nodes; i++)
  107.     {
  108.         sasiad = graf[i];
  109.         while (sasiad)
  110.         {
  111.             backup = sasiad;
  112.             sasiad = sasiad->next;
  113.             delete backup;
  114.         }
  115.     }
  116.     delete[] graf;
  117.  
  118.     getchar();
  119.     getchar();
  120.     return 0;
  121. }
Add Comment
Please, Sign In to add comment