Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define INF 1000000
  3.  
  4. using namespace std;
  5.  
  6. double AdjMatrix[100][100];
  7. double AdjMatrix2[100][100];
  8. double parent[100][100];
  9. int distance;
  10. int pathfind(int src,int dest)
  11. {
  12.     if(src==dest)
  13.     {
  14.         cout<<"path "<<src;
  15.         return 0;
  16.     }
  17.  
  18.     pathfind(src,parent[src][dest]);
  19.     cout<<" "<<dest;
  20. }
  21. int main()
  22. {
  23.     ifstream input("Flyod_warshel.txt");
  24.     if(!input.is_open())
  25.     {
  26.         cout<<"File is not open"<<endl;
  27.     }
  28.     int totalnode,node1,node2;
  29.     double weight;
  30.     input>>totalnode;
  31.  
  32.     for(int i=1;i<=totalnode;i++)
  33.     {
  34.         for(int j=1;j<=totalnode;j++)
  35.         {
  36.             if(i==j)
  37.                 AdjMatrix[i][j]=0;
  38.             else
  39.                 AdjMatrix[i][j]=INF;
  40.             parent[i][j]=i;
  41.  
  42.         }
  43.     }
  44.     while(input>>node1>>node2>>weight)
  45.     {
  46.            AdjMatrix[node1][node2]=log(weight);
  47.            AdjMatrix2[node1][node2]=weight;
  48.     }
  49.     for(int i=1;i<=totalnode;i++)
  50.     {
  51.         for(int j=1;j<=totalnode;j++)
  52.         {
  53.             cout<<AdjMatrix[i][j]<<" ";
  54.         }
  55.         cout<<endl;
  56.     }
  57.     cout<<endl;
  58.     for(int i=1;i<=totalnode;i++)
  59.     {
  60.         for(int j=1;j<=totalnode;j++)
  61.         {
  62.             cout<<parent[i][j]<<" ";
  63.         }
  64.         cout<<endl;
  65.     }
  66.     for(int k=1;k<=totalnode;k++)
  67.     {
  68.         for(int i=1;i<=totalnode;i++)
  69.         {
  70.             for(int j=1;j<=totalnode;j++)
  71.             {
  72.                 if(AdjMatrix[i][k]+AdjMatrix[k][j]<AdjMatrix[i][j])
  73.                 {
  74.                     AdjMatrix2[i][j]=AdjMatrix2[i][k]*AdjMatrix2[k][j];
  75.                     AdjMatrix[i][j]=AdjMatrix[i][k]+AdjMatrix[k][j];
  76.                     parent[i][j]=parent[k][j];
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     int src;
  82.     int dest;
  83.     cout<<"ENter src and dest"<<endl;
  84.     cin>>src>>dest;
  85.     for(int i=1;i<=totalnode;i++)
  86.     {
  87.         for(int j=1;j<=totalnode;j++)
  88.         {
  89.             cout<<AdjMatrix[i][j]<<" ";
  90.         }
  91.         cout<<endl;
  92.     }
  93.     cout<<"Distance "<<AdjMatrix2[src][dest]<<endl;
  94.     pathfind(src,dest);
  95.  
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement