Advertisement
peterzig

kod dla Martyn

Nov 3rd, 2020
1,975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. // A C++ program for Dijkstra's single source shortest path algorithm.
  2. // The program is for adjacency matrix representation of the graph
  3.  
  4. #include <limits.h>
  5. #include <stdio.h>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <conio.h>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. // Number of vertices in the graph
  14. #define V 9
  15.  
  16. // A utility function to find the vertex with minimum distance value, from
  17. // the set of vertices not yet included in shortest path tree
  18. int minDistance(int dist[], bool sptSet[])
  19. {
  20.     // Initialize min value
  21.     int min = INT_MAX, min_index;
  22.  
  23.     for (int v = 0; v < V; v++)
  24.         if (sptSet[v] == false && dist[v] <= min)
  25.             min = dist[v], min_index = v;
  26.  
  27.     return min_index;
  28. }
  29.  
  30. // A utility function to print the constructed distance array
  31. void printSolution(int dist[])
  32. {
  33.     printf("Wezel \t\t Odleglosc\n");
  34.     for (int i = 0; i < V; i++)
  35.         printf("%d \t\t %d\n", i, dist[i]);
  36. }
  37.  
  38. // Function that implements Dijkstra's single source shortest path algorithm
  39. // for a graph represented using adjacency matrix representation
  40. void dijkstra(int graph[V][V], int src)
  41. {
  42.     int dist[V]; // The output array. dist[i] will hold the shortest
  43.     // distance from src to i
  44.  
  45.     bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest
  46.     // path tree or shortest distance from src to i is finalized
  47.  
  48.     // Initialize all distances as INFINITE and stpSet[] as false
  49.     for (int i = 0; i < V; i++)
  50.         dist[i] = INT_MAX, sptSet[i] = false;
  51.  
  52.     // Distance of source vertex from itself is always 0
  53.     dist[src] = 0;
  54.  
  55.     // Find shortest path for all vertices
  56.     for (int count = 0; count < V - 1; count++) {
  57.         // Pick the minimum distance vertex from the set of vertices not
  58.         // yet processed. u is always equal to src in the first iteration.
  59.         int u = minDistance(dist, sptSet);
  60.  
  61.         // Mark the picked vertex as processed
  62.         sptSet[u] = true;
  63.  
  64.         // Update dist value of the adjacent vertices of the picked vertex.
  65.         for (int v = 0; v < V; v++)
  66.  
  67.             // Update dist[v] only if is not in sptSet, there is an edge from
  68.             // u to v, and total weight of path from src to v through u is
  69.             // smaller than current value of dist[v]
  70.             if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
  71.                 && dist[u] + graph[u][v] < dist[v])
  72.                 dist[v] = dist[u] + graph[u][v];
  73.     }
  74.  
  75.     // print the constructed distance array
  76.     printSolution(dist);
  77. }
  78.  
  79. // driver program to test above function
  80. int main(int argc, char* argv[2])
  81. {
  82.     argv[0] == "-i";
  83.     argv[1] == "-o";
  84.     argv[2] == "-s";
  85.  
  86.     /* Let us create the example graph discussed above */
  87.     int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
  88.                         { 4, 0, 8, 0, 0, 0, 0, 11, 0 },
  89.                         { 0, 8, 0, 7, 0, 4, 0, 0, 2 },
  90.                         { 0, 0, 7, 0, 9, 14, 0, 0, 0 },
  91.                         { 0, 0, 0, 9, 0, 10, 0, 0, 0 },
  92.                         { 0, 0, 4, 14, 10, 0, 2, 0, 0 },
  93.                         { 0, 0, 0, 0, 0, 2, 0, 1, 6 },
  94.                         { 8, 11, 0, 0, 0, 0, 1, 0, 7 },
  95.                         { 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
  96.  
  97.     dijkstra(graph, 0);
  98.  
  99.     if (argc == 1)
  100.     {
  101.         printf("Nie podano argumentow!\n Mozliwe argumenty to: \n-i sciezka do pliku wejsciowego \n-o sciezka do pliku wyjsciowego \n-s miasto startowe \n");
  102.     }
  103.     if (argc >= 2)
  104.     {
  105.         if (argv[0]) {
  106.             printf("wybrano '-i'");
  107.         }
  108.         if (argv[1]) {
  109.             printf("wybrano '-o'");
  110.         }
  111.         if (argv[2]) {
  112.             printf("wybrano '-s'");
  113.         }
  114.         printf("\nPodano liczbe argumentow: %d", argc);
  115.     }
  116.  
  117.     fstream plik, plik2;
  118.     plik.open("odleglosci.txt", ios::in);
  119.     plik2.open("trasy.txt", ios::out);
  120.  
  121.     if (plik.good())
  122.     {
  123.         string napis;
  124.         printf("Zawartosc pliku: \n");
  125.         while (!plik.eof())
  126.         {
  127.             getline(plik, napis);
  128.             cout << napis << endl;
  129.         }
  130.         plik.close();
  131.     }
  132.     else printf("Error! Nie udalo otworzyc sie pliku!");
  133.  
  134.  
  135.     if (plik2.good())
  136.     {
  137.         for (int i = 0; i < 10; i++)
  138.         {
  139.             plik2 << i << " ";
  140.             plik2.flush();
  141.         }
  142.  
  143.         plik2.close();
  144.     }
  145.  
  146.     getchar();
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement