首页 > 代码库 > dijkstra

dijkstra

dijkstra时间复杂度 O(|E| *log|V|)

 1 const int MAX_E = 2005; 2 const int MAX_V = 1005; 3 const int INF = 1e8; 4  5 int V, E; 6 struct edge { 7     int to, cost; 8 }; 9 vector<edge>G[MAX_V];10 typedef pair<int, int>P;11 int d[MAX_V];12 13 void dijstra(int s) {14     priority_queue<P, vector<P>, greater<P> >que;15     fill(d, d+V, INF);16     d[s] = 0;17     que.push(P(0, s));18     while(!que.empty()) {19         P p = que.top();20         que.pop();21         int v = p.second;22         if(d[v] < p.first) continue;23         for(int i = 0; i < G[v].size(); i++) {24             edge e = G[v][i];25             if(d[e.to] > d[v] + e.cost) {26                 d[e.to] = d[v] + e.cost;27                 que.push(P(d[e.to], e.to));28             }29         }30     }31 }

 

dijkstra