Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define M 110
- VII adjList[M];//node, edeg_cost
- int dist[M];//you can make it local to function
- int nodes, edges;
- struct data{//kinda of edges
- int city, dist;
- data(int dist, int city){ this->city = city, this->dist = dist; }
- bool operator < (const data& rhs) const {
- return rhs.dist < this->dist;//ultai dilam..katakati :)
- }
- };
- int dijkstra(int source, int destination){
- FOR(i, 1, nodes){
- dist[i] = INF;
- //sort(ALL(adjList[i]), [](const PII& a, const PII& b){ return a.second < b.second; });
- }
- priority_queue< data > pq;
- pq.push(data(0, source));
- dist[source] = 0;
- while(!pq.empty()){
- data u = pq.top();
- pq.pop();
- int uCost = dist[u.city];
- for(auto& item: adjList[u.city]){
- data v(item.second + uCost, item.first);
- if(dist[v.city] > v.dist){
- dist[v.city] = v.dist;
- pq.push(v);
- }
- }
- }
- return dist[destination];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement