Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define INF 1000000
- #include <chrono>
- using namespace std;
- int a,b,w,y,t=0;
- int edge,node;
- vector<int>vis;
- bool v[50];
- void process(int adj[][50],int x)
- {
- cout<<"Shortest path : ";
- while(vis.size()!=node)
- {
- int m=INF;
- cout<<x<<" ";
- for(int j=1; j<=node; j++)
- {
- if(t==j)
- continue;
- m=min(m,adj[x][j]);
- }
- y=m;
- for(int i=1; i<=node; i++)
- {
- if(adj[x][i]==y)
- {
- if(v[i])
- continue;
- v[i]=true;
- vis.push_back(i);
- t=x;
- x=i;
- }
- }
- }
- cout<<endl;
- }
- int main()
- {
- int adj[50][50],p;
- using namespace std::chrono;
- cout<<"Enter number of node & edges :\n";
- cin>>node>>edge;
- cout<<"Enter two vertices & weight between them "<<endl;
- for(int i=1; i<=edge; i++)
- {
- cin>>a>>b>>w;
- adj[a][b]=w;
- adj[b][a]=w;
- }
- high_resolution_clock::time_point t1 = high_resolution_clock::now();
- for(int i=1; i<=node; i++)
- {
- for(int j=1; j<=node; j++)
- {
- if(!adj[i][j])
- adj[i][j]=INF;
- }
- }
- process(adj,1);
- high_resolution_clock::time_point t2 = high_resolution_clock::now();
- duration<double> time_span = duration_cast<microseconds>(t2 - t1);
- printf("\nUsing Adjacent Matrix = %0.5f seconds. \n",time_span.count() );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement