Advertisement
Mostafizur_Rahman

matrix

Aug 14th, 2021
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define INF 1000000
  3. #include <chrono>
  4. using namespace std;
  5. int a,b,w,y,t=0;
  6. int edge,node;
  7. vector<int>vis;
  8. bool v[50];
  9. void process(int adj[][50],int x)
  10. {
  11.     cout<<"Shortest path : ";
  12.     while(vis.size()!=node)
  13.     {
  14.         int m=INF;
  15.         cout<<x<<" ";
  16.         for(int j=1; j<=node; j++)
  17.         {
  18.             if(t==j)
  19.                 continue;
  20.             m=min(m,adj[x][j]);
  21.         }
  22.         y=m;
  23.         for(int i=1; i<=node; i++)
  24.         {
  25.             if(adj[x][i]==y)
  26.             {
  27.                 if(v[i])
  28.                     continue;
  29.                 v[i]=true;
  30.                 vis.push_back(i);
  31.                 t=x;
  32.                 x=i;
  33.             }
  34.         }
  35.  
  36.     }
  37.     cout<<endl;
  38.  
  39. }
  40. int main()
  41. {
  42.     int adj[50][50],p;
  43.     using namespace std::chrono;
  44.     cout<<"Enter number of node & edges :\n";
  45.     cin>>node>>edge;
  46.     cout<<"Enter two vertices & weight between them "<<endl;
  47.     for(int i=1; i<=edge; i++)
  48.     {
  49.         cin>>a>>b>>w;
  50.         adj[a][b]=w;
  51.         adj[b][a]=w;
  52.     }
  53.     high_resolution_clock::time_point t1 = high_resolution_clock::now();
  54.     for(int i=1; i<=node; i++)
  55.     {
  56.         for(int j=1; j<=node; j++)
  57.         {
  58.             if(!adj[i][j])
  59.                 adj[i][j]=INF;
  60.         }
  61.     }
  62.     process(adj,1);
  63.     high_resolution_clock::time_point t2 = high_resolution_clock::now();
  64.  
  65.     duration<double> time_span = duration_cast<microseconds>(t2 - t1);
  66.     printf("\nUsing Adjacent Matrix = %0.5f seconds. \n",time_span.count() );
  67.  
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement