_dijkstra.c
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:3k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _dijkstra.c
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. /*******************************************************************************
  12. *                                                                              *
  13. *  DIJKSTRA  (single source shortest paths)                                    *
  14. *                                                                              *
  15. *******************************************************************************/
  16. #include <LEDA/graph_alg.h>
  17. #include <LEDA/node_pq.h>
  18. void DIJKSTRA(const graph& G, node s, const edge_array<num_type>& cost,
  19.                                             node_array<num_type>& dist,
  20.                                             node_array<edge>& pred )
  21. { /* 
  22.      computes single source shortest paths from node s for 
  23.      a non-negative network (G,cost), computes for all nodes v:
  24.      a) dist[v] = cost of shortest path from s to v
  25.      b) pred[v] = predecessor edge of v in shortest paths tree
  26.   */
  27.   node_pq<num_type>  PQ(G);
  28.   node v;                                                                    
  29.   edge e;                                                                      
  30.                                                                                
  31.   forall_nodes(v,G)                                                            
  32.   { pred[v] = nil;                                                             
  33.     dist[v] = max_num;                                                      
  34.    }                                                                         
  35.   dist[s] = 0;
  36.   PQ.insert(s,0);
  37.                                                                                
  38.   while (! PQ.empty())
  39.   { node u = PQ.del_min();
  40.     num_type du = dist[u];
  41.     forall_adj_edges(e,u)                                                    
  42.     { v = target(e);                                                      
  43.       num_type c = du + cost[e];                                              
  44.       if (c < dist[v])                                                    
  45.       { if (dist[v] == max_num) 
  46.            PQ.insert(v,c);
  47.         else 
  48.            PQ.decrease_p(v,c);
  49.         dist[v] = c;                                                     
  50.         pred[v] = e;                                                     
  51.        }                                                                 
  52.      }                                                                    
  53.    } 
  54. }