Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int a,b,w,y,t=0;
- int edge=6,node=5;
- vector<int>vis;
- bool v[50];
- void process(int adj[][50],int x)
- {
- cout<<"Shortest path : ";
- while(vis.size()!=node)
- {
- int m=1000000;
- 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;
- 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;
- }
- for(int i=1; i<=edge; i++)
- {
- for(int j=1; j<=edge; j++)
- {
- if(!adj[i][j])
- adj[i][j]=1000000;//think like INF
- }
- }
- cout<<"Enter starting node : ";
- cin>>p;
- process(adj,p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement