Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include<stdio.h>
  5. using namespace std;
  6.  
  7. void add(int nod,int val);
  8. void rmv(int nod);
  9. void sift(int nod);
  10. void percolate(int nod);
  11. void swap_nodes(int nod1,int nod2);
  12. void dijkstra(int start);
  13.  
  14. struct
  15. {
  16. int nod,cost;
  17. }heap[50001];
  18. int N=0;
  19. int shortestPath[50001];
  20. int poz[50001];
  21. class Muchie {
  22. public:
  23. int Vec;
  24. int Cost;
  25. Muchie () {
  26. Vec = 0;
  27. Cost = 0;
  28. }
  29. Muchie (int v, int c) {
  30. Vec = v;
  31. Cost = c;
  32. }
  33. };
  34.  
  35. vector<Muchie> v[50001];
  36.  
  37. bool DoubleChain;
  38. int Chain[50001];
  39. int Chain2[50001];
  40. int ChainLevel;
  41. int Chain2Level;
  42.  
  43. int main () {
  44. //freopen("dijkstra.in","r",stdin);
  45. //freopen("dijkstra.out","w",stdout);
  46. cin.tie(0);
  47. ios::sync_with_stdio(false);
  48. int Noduri, Muchii,Ka;
  49. cin >> Noduri >> Ka>>Muchii;
  50.  
  51. for (int i = 0; i < Muchii; i++) {
  52. int orig, dest, cost;
  53. cin >> orig >> dest >> cost;
  54. v[orig].push_back(Muchie(dest, cost));
  55. v[dest].push_back(Muchie(orig,cost));
  56. }
  57. shortestPath[1]=0;
  58. add(1,0);
  59.  
  60. for(int i=2;i<=Noduri;i++)
  61. {
  62. shortestPath[i]=1000000000;
  63. add(i,1000000000);
  64. }
  65.  
  66. while(N>0)
  67. {
  68. int nod=heap[1].nod;
  69. int cost=heap[1].cost;
  70. rmv(1);
  71.  
  72. for(int i=0;i<v[nod].size();i++)
  73. {
  74. int vec=v[nod][i].Vec;
  75. int cmp;
  76. if((shortestPath[nod]/Ka)%2==1)
  77. {
  78. cmp=(shortestPath[nod]/Ka)*Ka+Ka;
  79. }
  80. else
  81. {
  82. cmp=shortestPath[nod];
  83. }
  84. if(shortestPath[vec] > cmp+ v[nod][i].Cost)
  85. {
  86. shortestPath[vec]=v[nod][i].Cost+cmp;
  87. heap[poz[vec]].cost=shortestPath[vec];
  88. percolate(poz[vec]);
  89. }
  90. }
  91. }
  92. std::cout<<shortestPath[Noduri];
  93. }
  94.  
  95. void add(int nod,int val)
  96. {
  97.  
  98. heap[++N].cost=val;
  99. heap[N].nod=nod;
  100. poz[nod]=N;
  101. percolate(N);
  102. }
  103.  
  104. void rmv(int nod)
  105. {
  106. swap_nodes(nod,N);
  107. N--;
  108.  
  109. if(nod>1 && heap[nod].cost<heap[nod/2].cost)
  110. {
  111. percolate(nod);
  112. }
  113. else
  114. {
  115. sift(nod);
  116. }
  117. }
  118.  
  119. void sift(int nod)
  120. {
  121. int son=0;
  122. do
  123. {
  124. son=0;
  125. if(2*nod+1<=N)
  126. {
  127. if(heap[2*nod+1].cost<heap[2*nod].cost)
  128. {
  129. son=2*nod+1;
  130. }
  131. else
  132. {
  133. son=2*nod;
  134. }
  135. }
  136. else if(2*nod<=N)
  137. {
  138. son=2*nod;
  139. }
  140. if(heap[nod].cost<heap[son].cost)
  141. {
  142. son=0;
  143. }
  144. if(son)
  145. {
  146. swap_nodes(nod,son);
  147. nod=son;
  148. }
  149. }while(son);
  150. }
  151.  
  152. void percolate(int nod)
  153. {
  154. while(nod>1 && heap[nod].cost<heap[nod/2].cost)
  155. {
  156. swap_nodes(nod,nod/2);
  157. nod/=2;
  158. }
  159. }
  160.  
  161. void swap_nodes(int nod1,int nod2)
  162. {
  163. std::swap(poz[heap[nod1].nod],poz[heap[nod2].nod]);
  164. std::swap(heap[nod1].nod,heap[nod2].nod);
  165. std::swap(heap[nod1].cost,heap[nod2].cost);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement