Instantly share code, notes, and snippets.

# ravikiran0606/SP in Weighted Graph.cpp

Created September 25, 2016 15:02
Show Gist options
• Save ravikiran0606/90760eb3d330b8b03f619c1cbe47bceb to your computer and use it in GitHub Desktop.
Shortest Path in Weighted Graph : (Using Dijkstra)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #include using namespace std; vectord; vector< vector< pair > >graph; vectormark; void dijkstra(int s,int n){ int u,i; d[s]=0; pairp; set< pair >st; for(int i=1;i<=n;i++) st.insert(make_pair(d[i],i)); while(!st.empty()){ u=st.begin()->second; mark[u]=true; st.erase(st.begin()); for(i=0;i>n; mark.resize(n+1); graph.resize(n+1); d.resize(n+1); cout<<"\nEnter the number of edges..."; cin>>m; cout<<"\nEnter the edges along with their weights...\n"; for(i=1;i<=m;i++){ cin>>u>>v>>w; graph[u].push_back(make_pair(v,w)); graph[v].push_back(make_pair(u,w)); } // Calculating the single source shortest paths... fill_n(mark.begin(),n+1,false); fill_n(d.begin(),n+1,INT_MAX); cout<<"\nEnter the source and destination..."; cin>>u>>v; dijkstra(u,n); cout<<"\nThe Shortest distance is..."<

### okarim4259 commented Apr 14, 2018

Hi would you happen to know how to print the shortest path from this algorithm?