SHARE
TWEET

Untitled

a guest Feb 27th, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<fstream>
  2. #include<iostream>
  3. #include<iomanip>
  4. using namespace std;
  5.  
  6. int a[20][20],n;
  7. #define pinfinit 1000000
  8. void citire_cost()
  9. {ifstream f("ro.in");
  10.  int i,j,c;
  11.   f>>n;
  12.  for(i=1;i<=n;i++)
  13.     for(j=1;j<=n;j++)
  14.       if(i==j)
  15.              a[i][j]=0;
  16.        else
  17.              a[i][j]=pinfinit;
  18.  
  19.      while(f>>i>>j>>c)
  20.       a[i][j]=a[j][i]=c;}
  21.  
  22.  
  23. void afisare_mat()
  24.  
  25. {for(int i=1;i<=n;i++)
  26.    {for(int j=1;j<=n;j++)
  27.              if(a[i][j]==pinfinit)
  28.                 cout<<setw(10)<<"pinfinit";
  29.              else
  30.                cout<<setw(10)<<a[i][j];
  31.  
  32.     cout<<endl;}
  33. }
  34.  
  35. void drumuri_optime()
  36. {for(int k=1;k<=n;k++)
  37.     for(int i=1;i<=n;i++)
  38.        for(int j=1;j<=n;j++)
  39.                if(a[i][j]>a[i][k]+a[k][j])
  40.                               a[i][j]=a[i][k]+a[k][j];
  41. }
  42.  
  43. void drum(int i,int j)
  44. {int g=0,k=1;
  45.  while(k<=n&&!g)
  46. {if(i!=k&&j!=k)
  47. if(a[i][j]==a[i][k]+a[k][j])
  48. {drum(i,k);
  49. drum(k,j);
  50. g=1;}
  51. k++;
  52. }
  53.   if(!g)
  54.      cout<<j<<" ";
  55. }
  56. void scriu_drum(int x,int y)
  57. {if(a[x][y]<pinfinit)
  58.      {cout<<"drumul de la "<<x<<" la "<<y<<" are lungimea "<<a[x][y];
  59.       cout<<endl<<"un drum optim este: "<<endl;
  60.       cout<<x<<" ";
  61.       drum(x,y);
  62.      }
  63.  else
  64.      cout<<"nu exista drum de la "<<x<<" la "<<y;
  65.  
  66. }
  67.  
  68. int main()
  69. {int x,y;
  70. citire_cost();
  71. cout<<endl<<"matricea ponderilor "<<endl;
  72. afisare_mat();
  73. drumuri_optime();
  74. cout<<endl<<"matricea drumurilor optime "<<endl;
  75. afisare_mat();
  76. cout<<endl<<"Se determina drumul minim intre varfurile x si y "<<endl;
  77. cout<<"x=";cin>>x;
  78. cout<<"y=";cin>>y;
  79. scriu_drum(x,y);
  80. return 0;
  81.  
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top