Advertisement
midnight_sun

Untitled

Nov 17th, 2023
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. //
  2. //  dijkstra.cpp
  3. //  console2
  4. //
  5.  
  6. /* priority_queue 큐를 이용하여 Dijkstra 최소 비용 거리(경로) 구하기
  7.  */
  8.  
  9. #include <vector>
  10. #include <queue>
  11. #include <iostream>
  12. #include <fstream>
  13. #include <sstream>
  14.  
  15. #include <stdio.h>
  16. #include <math.h>
  17.  
  18. using namespace std;
  19.  
  20. // 무한대값 정의
  21. #define INF 10000000
  22.  
  23. int NO_PARENT = -1;
  24.  
  25. // pair<int, int> 에 대한 intPair 타입 정의
  26. typedef pair<int, int> intPair;
  27.  
  28. void printPath(int currentVertex, vector<int> parents)
  29. {
  30.     if ( currentVertex == NO_PARENT )
  31.     {
  32.         return;
  33.     }
  34.     printPath(parents[currentVertex], parents);
  35.     cout << currentVertex << " ";
  36. }
  37.  
  38. // 인접한 두지점(노드)의 경로 비용(거리 값) 추가
  39. void add_node(vector <pair<int, int> > adj_nodes[], int a, int b, int dist)
  40. {
  41.     // s 지점(노드)에 e 지점(노드)과 거리 값 추가
  42.     adj_nodes[a].push_back(make_pair(b, dist));
  43.     // e 지점(노드)에 s 지점(노드)과 거리 값 추가
  44.     adj_nodes[b].push_back(make_pair(a, dist));
  45. }
  46.  
  47. // 최단 거리 (최소 비용) 경로를 시작 지점으로부터 구함
  48. // Prints shortest paths from start to all other vertices
  49. int get_shortest_distance(vector<pair<int,int> > adj_nodes[], size_t nodes_num,
  50.                            int start, int end)
  51. {
  52.     // 전처리중인 정점을 저장할 priority queue 를 만든다
  53.     priority_queue< intPair, vector <intPair> , greater<intPair> > pq;
  54.  
  55.     // 거리에 대한 정점을 만들고 최대 거리값 infinite(INF)으로 초기화
  56.     vector<int> dist(nodes_num, INF);
  57.  
  58.     // 처음 시작 노드는 거리가 0 이므로 0으로 설정해서 넣는다(추가)
  59.     pq.push(make_pair(start, 0));
  60.     dist[start] = 0;
  61.  
  62.     // Parent array to store shortest
  63.     // path tree
  64.     vector<int> parents(nodes_num);
  65.  
  66.     // The starting vertex does not
  67.     // have a parent
  68.     for ( int i = 0; i < nodes_num; i++ )
  69.     {
  70.         parents[i] = NO_PARENT;
  71.     }
  72.  
  73.     // priority queue 가 없을 때까지(모든 노드에 대한 거리가 설정될 때까지) 반복
  74.     while ( !pq.empty() )
  75.     {
  76.         // priority queue 첫번째 정점 노드는 최소 거리의 정점이며,
  77.         // priority queue 에서 추출한다.
  78.         // pair 에서 첫번째(first)가 정점의 이름(경로 지점 명칭)이며 두번째(second)가 거리 값
  79.         int node_idx = pq.top().first;
  80.         pq.pop();
  81.  
  82.         // node_idx 의 모든 인접 노드를 구한다
  83.         for ( auto x : adj_nodes[node_idx] )
  84.         {
  85.             // node_idx 노드의 현재 인접한 노드의 정점의 이름과 거리값을 구한다
  86.             int idx = x.first; // 노드
  87.             int len = x.second; // 거리
  88.  
  89.             // idx 에서 node_idx 까지의 짧은 경로가 있으면
  90.             if ( dist[idx] > dist[node_idx] + len )
  91.             {
  92.                 // idx 거리를 업데이트 한다
  93.                 dist[idx] = dist[node_idx] + len;
  94.                 if ( idx != end )
  95.                 {
  96.                     pq.push(make_pair(idx, dist[idx]));
  97.                 }
  98.                
  99.                 parents[idx] = node_idx;
  100.                 //printf("idx: %d\n", idx);
  101.             }
  102.         }
  103.     }
  104.  
  105.     printf("Result: %d to %d, cost: %d\n", start, end, dist[end]);
  106.     printPath(end, parents);
  107.     printf("\n");
  108.  
  109.     return dist[end];
  110. }
  111.  
  112. int main(void)
  113. {
  114.     size_t nodes_num;
  115.     int start, end, sum = 0;
  116.  
  117.     nodes_num = 9;
  118.     vector<intPair > *adj_nodes = new vector<intPair >[nodes_num];
  119.  
  120.     // making above shown graph
  121.     add_node(adj_nodes, 0, 1, 4);
  122.     add_node(adj_nodes, 0, 7, 8);
  123.     add_node(adj_nodes, 1, 2, 8);
  124.     add_node(adj_nodes, 1, 7, 11);
  125.     add_node(adj_nodes, 2, 3, 7);
  126.     add_node(adj_nodes, 2, 8, 2);
  127.     add_node(adj_nodes, 2, 5, 4);
  128.     add_node(adj_nodes, 3, 4, 9);
  129.     add_node(adj_nodes, 3, 5, 14);
  130.     add_node(adj_nodes, 4, 5, 10);
  131.     add_node(adj_nodes, 5, 6, 2);
  132.     add_node(adj_nodes, 6, 7, 1);
  133.     add_node(adj_nodes, 6, 8, 6);
  134.     add_node(adj_nodes, 7, 8, 7);
  135.    
  136.     start = 4;
  137.     end = 8;
  138.     sum += get_shortest_distance(adj_nodes, nodes_num, start, 0);
  139.     sum += get_shortest_distance(adj_nodes, nodes_num, 0, end);
  140.  
  141.     printf("The distance between %d and %d via 0 is: %d\n", start, end, sum);
  142.    
  143.     delete[] adj_nodes;
  144.  
  145.     return 0;
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement