Advertisement
Guest User

Untitled

a guest
May 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. //============================================================================
  2. // Name        : Floyd_Warshallov_algoritem.cpp
  3. // Author      : Jasmin Dervisevic
  4. // Version     :
  5. // Copyright   : Jasmin Dervisevic all rights reserved
  6. // Description : Vaja 5, Floyd_Warhallov_algoritem
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <time.h>
  11. #include <fstream>
  12. #include <stdlib.h>
  13. #include <chrono>
  14. using namespace std;
  15.  
  16. int vel;
  17.  
  18.  
  19. void BERI(int C[100][100], string f) {
  20.     fstream dat(f.c_str(), fstream::in);
  21.     if (dat.is_open()) {
  22.         dat >> vel;
  23.           int i=0;
  24.           int p, q, cena;
  25.           while(!dat.eof())
  26.           {
  27.             dat >> p >> q >> cena;
  28.             C[p-1][q-1] = cena;
  29.             i++;
  30.           }
  31.  
  32.         dat.close();
  33.  
  34.     }else {
  35.         cout<<"Napaka pri odpiranju datoteke!"<<endl;
  36.     }
  37. }
  38. void Floyd(int C[100][100]) {
  39.     int n;
  40.     n = vel + 1;
  41.     int P[vel][vel];
  42.     int D[vel][vel];
  43.     for(int i = 0; i < n-1; i++) {
  44.         for(int j = 0; j < n - 1; j++) {
  45.             D[i][j] = C[i][j];
  46.             if (i != j && C[i][j] != 10000 /*INFINITY???*/) {
  47.                 P[i][j] = i;
  48.  
  49.             }else {
  50.                 P[i][j] = -10000; //NIL??;
  51.             }
  52.         }
  53.  
  54.     }
  55.     for(int k = 0; k < n - 1; k++) {
  56.         for(int i = 0; i < n - 1; i++) {
  57.             for(int j = 0; j < n - 1; j++) {
  58.                 if(D[i][j] > D[i][k] + D[k][j]) {
  59.                     D[i][j] = D[i][k] + D[k][j];
  60.                     P[i][j] = P[k][j];
  61.                 }
  62.             }
  63.         }
  64.     }
  65.  
  66.     //return D, P;
  67.  
  68. }
  69.  
  70. void Izpis_Poti (int P[][vel], int s, int g) {
  71.     if (s == g) {
  72.         cout<<"Zacetno vozlisce: " << s << endl;
  73.  
  74.     }else {
  75.         if(P[s][g] < 0 /*NIL*/) {
  76.             cout<< "Med "<< s << " in " << g << " ni poti."<<endl;
  77.  
  78.         }else {
  79.             Izpis_Poti(P, s, P[s][g]);
  80.             cout<< "Koncno vozlisce: "<< g <<endl;
  81.         }
  82.     }
  83. }
  84. int main() {
  85.     int p[vel][vel];
  86.     int n;
  87.     int koncno_vozlisce, ciljno_vozlisce;
  88.     while (n!=4) {
  89.         cout<<"Floyd - Warshallov algoritem - izbira: "<<endl;
  90.         cout<< "1. Preberi graf iz datoteke"<<endl;
  91.         cout<< "2. Zagon algoritma"<<endl;
  92.         cout<< "3. Izpis najkrajse poti med vozliscema s in g"<<endl;
  93.         cout<< "4. Konec"<<endl;
  94.         cout<<"Prosim, vnesi stevilko pred menijem: ";
  95.                 cin>> n;
  96.  
  97.         switch(n) {
  98.         case 1: {
  99.             BERI(p, "Testni_graf.txt");
  100.             cout<<"-------------------------------------"<<endl;
  101.             break;
  102.         }
  103.         case 2: {
  104.             Floyd(p);
  105.             break;
  106.         }
  107.         case 3: {
  108.             //nekaj pac;
  109.             cout<<"Vnesi koncno vozlisce: ";
  110.             cin>>koncno_vozlisce;
  111.             cout<<"Vnesi ciljno vozlisce: ";
  112.             cin>>ciljno_vozlisce;
  113.             Izpis_Poti(p, koncno_vozlisce, ciljno_vozlisce);
  114.             break;
  115.         }
  116.         case 4: {
  117.             exit(0);
  118.             break;
  119.         }
  120.             default:
  121.                 cout<<"Napacen vnos, poskusi znova!"<<endl;
  122.  
  123.             }
  124.  
  125.         }
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement