You are not logged in. Please login at www.codechef.com to post your questions!

×

# Dijkstar's Algo to find shortest path from 1 to N

 2 In The Dijkstra's algo we need to extract the minimum of D where D is the distance from node 0. But as D gets updated with the distance randomly how could we extract the minimum. Eg: Let at an instance we have the below array and we have used the node 0(its distance to itself is always 0) and accordingly its adjacent node2 and node5 get updated with distance from node 0. D: 0 INF 3 INF INF 1 INF If we use O(n) then ultimately we are using O(n^2) as we will be updating the adjacent of node 5 and again extracting the minimum by using O(n). Is there a better approach to extract the minimum?? asked 28 Jun '15, 14:00 35●2●13 accept rate: 0%

 3 You can refer here http://zobayer.blogspot.de/2009/12/dijkstras-algorithm-in-c.html answered 29 Jun '15, 18:52 4★pallesai 176●8●31 accept rate: 17%
 2 Yes..use heap to store these. Priority queue is the key. answered 28 Jun '15, 14:26 696●1●12 accept rate: 17%
 2 void dijkstras(int start,vector< pii > G[],int d[]) { int u,c,v,w; priority_queue< pii, vector< pii >, greater< pii > > Q; memset(d, 0x3f, sizeof d); Q.push(pii(0,start)); d[start]=0; while(!Q.empty()) { c=Q.top().first; u=Q.top().second; Q.pop(); if(d[u]d[u]+w) { d[v]=d[u]+w; Q.push(pii(d[v],v)); } } } }  answered 28 Jun '15, 14:29 696●1●12 accept rate: 17% here G is vector> G[]. every vertice u will have a pair v(edge u->v) and w(weight). (28 Jun '15, 14:31)
 2 It was helpful answered 19 Nov '16, 20:42 2★lud1161 14●18 accept rate: 58%
 2 it was helpful answered 19 Nov '16, 21:01 37●2 accept rate: 0%
 2 Answer is hidden as author is suspended. Click here to view. answered 19 Nov '16, 22:47 (suspended) accept rate: 0%
 2 Answer is hidden as author is suspended. Click here to view. answered 19 Nov '16, 22:49 (suspended) accept rate: 0%
 0 I think function can be helpful. Only integer -1 in case of no path. void dijkstra(int start, vectorG[]) { long long int dist,prev; priority_queue,greater >q; vectorpath; for(int i=1; i<=n; i++) { dist[i]=infinity; prev[i]=-1; } q.push(pii(0,start)); dist[start]=0; while(!q.empty()) { u=q.top().second; q.pop(); for(int i=0; i=0; i--) { cout<
 0 The trick here is to use a priority queue , where each and every distance would be pushed. Next we will eject elements one by one , note that if the distance is already ejected and copied in the minimum distance vector , it will be ejected but not copied into the min distance vector , here is an implementation of it , I made for Free Tickets (INOI 2012) problem, `int dijkstra(std::vector >graph,int vertex,int size){ std::vectormap(size,INT_MAX); std::priority_queueheap; std::vectorvisited(size,false); map[vertex] = 0; for(int i=0;i
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,236
×180

question asked: 28 Jun '15, 14:00

question was seen: 2,429 times

last updated: 24 Nov '16, 22:05