Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7. ifstream f ("dijkstra.in");
  8. ofstream g ("dijkstra.out");
  9. #define GMAX 50010
  10. #define inf 0x3f3f3f3f
  11.  
  12. #define father(x) (x>>1)
  13. #define left_son(x) (x<<1)
  14. #define right_son(x) ((x<<1) + 1)
  15.  
  16. struct Heap
  17. {
  18.     int nod;
  19.     int cost;
  20. }heap[GMAX];
  21.  
  22. int sz = 0;
  23. vector < pair<int,int> > G[GMAX];
  24. int dist[GMAX];
  25. void insert(int nod,int x)
  26. {
  27.     sz++;
  28.     heap[sz].nod = nod;
  29.     heap[sz].cost = x;
  30.     int i = sz;
  31.  
  32.     while(father(i) >=1 && heap[father(i)].cost > heap[i].cost)
  33.     {
  34.         swap(heap[father(i)].cost,heap[i].cost);
  35.         swap(heap[father(i)].nod,heap[i].nod);
  36.         i=father(i);
  37.     }
  38. }
  39. int getNod()
  40. {
  41.     return heap[1].nod;
  42. }
  43. int getCost()
  44. {
  45.     return heap[1].cost;
  46. }
  47. void pop()
  48. {
  49.     heap[1].nod = heap[sz].nod;
  50.     heap[1].cost = heap[sz].cost;
  51.     sz--;
  52.     int i = 1;
  53.     while(right_son(i) < sz &&
  54.     (heap[right_son(i)].cost < heap[i].cost || heap[left_son(i)].cost < heap[i].cost))
  55.     {
  56.         if(heap[right_son(i)].cost < heap[left_son(i)].cost)
  57.         {
  58.             swap(heap[i].cost,heap[right_son(i)].cost);
  59.             swap(heap[i].nod,heap[right_son(i)].nod);
  60.         }
  61.  
  62.         else if(heap[right_son(i)].cost > heap[left_son(i)].cost)
  63.         {
  64.              swap(heap[left_son(i)].cost,heap[i].cost);
  65.              swap(heap[left_son(i)].nod,heap[i].nod);
  66.         }
  67.  
  68.  
  69.         i=right_son(i);
  70.     }if(left_son(i) < sz && heap[i].cost > heap[left_son(i)].cost)
  71.     {
  72.         swap(heap[i].cost,heap[left_son(i)].cost);
  73.         swap(heap[i].nod,heap[left_son(i)].nod);
  74.     }
  75. }
  76. int main()
  77. {
  78.     int n,m;
  79.     f>>n>>m;
  80.     for(int i=1;i<=m;i++)
  81.     {
  82.         int x,y,z;
  83.         f>>x>>y>>z;
  84.         G[x].push_back(make_pair(y,z)); // orientat
  85.     }
  86.     memset(dist,0x3f,sizeof(dist));
  87.     insert(1,0);
  88.     while(sz >= 1)
  89.     {
  90.         int nod = getNod();
  91.         int cost = getCost();
  92.         pop();
  93.  
  94.         if(dist[nod] != inf && dist[nod] < cost)
  95.             continue;
  96.  
  97.         dist[nod] = cost;
  98.         //g<<nod<<' '<<cost<<endl;
  99.         for(int i=0;i<G[nod].size();i++)
  100.         {
  101.            insert(G[nod][i].first,cost + G[nod][i].second);
  102.         }
  103.     }
  104.  
  105.     for(int i=2;i<=n;i++)
  106.     {
  107.         if(dist[i] == inf) g<<-1<<' ';
  108.             else g<<dist[i]<<' ';
  109.     }
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement