Advertisement
Five_NT

[C++] Drumul minim intre doua puncte. (R.F.)

May 19th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. // Un turist vrea sa calatoreasca din orasul X in orasul Y. Calculati drumul minim
  2.  
  3. #include <fstream>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. ifstream f("A.in");
  9. ofstream g("B.out");
  10.  
  11. const long val = 99;
  12. int a[50][50], n, i, j, c, v[50], o, p;
  13.  
  14. void init()
  15. {
  16.     f >> n;
  17.     for(i=1; i<=n; i++)
  18.         for(j=1; j<=n; j++)
  19.             if(i == j)
  20.                 a[i][j] = 0;
  21.             else
  22.                 a[i][j] = val;
  23. }
  24.  
  25. void citire()
  26. {
  27.     while(f >> i >> j >> c)
  28.         a[i][j] = c;
  29.     do {
  30.         cout << "Nod plecarea: "; cin >> o;
  31.         cout << "Nod sosire: "; cin >> p;
  32.     } while( o > 0 && o < n && p > 0 && p < n);
  33. }
  34.  
  35. void afis()
  36. {
  37.     for(i=1; i<=n; i++)
  38.     {
  39.         for(j=1; j<=n; j++)
  40.         {
  41.             g << a[i][j] << "   ";
  42.         }
  43.         g << '\n';
  44.     }
  45. }
  46.  
  47. void Roy_Floyd()
  48. {
  49.     for(int k=1; k<=n; k++)
  50.         for(i=1; i<=n; i++)
  51.             for(j=1; j<=n; j++)
  52.                 if(a[i][k] + a[k][j] < a[i][j])
  53.                     a[i][j] = a[i][k] + a[k][j];
  54. }
  55.  
  56. int main()
  57. {
  58.     init();
  59.     citire();
  60.     Roy_Floyd();
  61.     afis();
  62.     cout << "* Distanta minima dintre orasele " << o << " - " << p << " este: " << a[o][p];
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement