Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. //DOPAJUL CONTINUA
  2. //ALGORITMUL LUI PRIM WOO
  3. #include<stdio.h>
  4. #include<climits>
  5. #include<algorithm>
  6. #include<vector>
  7. #define MAXV 200000
  8. #define MAXE 400000
  9. #define INF INT_MAX
  10. struct HipHipHooray
  11. {
  12. int vertex,cost;
  13. };
  14. struct Edge
  15. {
  16. int src,dst,cost;
  17. };
  18. //FUNCTII MULTE DE HEAPURI
  19. int left_son(int nod);
  20. int right_son(int nod);
  21. int father(int nod);
  22. void add(int vertex,int cost);
  23. void rmv(int nod);
  24. void sift(int nod);
  25. void percolate(int nod);
  26. void swap_nodes(int nod1,int nod2);
  27. void init();
  28. FILE*fin,*fout;
  29. HipHipHooray heap[MAXV+1];
  30. int pos[MAXV+1];
  31. int e[MAXV+1];
  32. bool in_heap[MAXV+1];
  33. Edge edges[MAXE+1];
  34. int sol[MAXE+1];
  35. std::vector<int> neighbors[MAXV+1];
  36. int N=0;
  37. int E,V;
  38. int main()
  39. {
  40. fin=fopen("apm.in","r");
  41. fout=fopen("apm.out","w");
  42. fscanf(fin,"%d%d",&V,&E);
  43. for(int i=1;i<=E;i++)
  44. {
  45. fscanf(fin,"%d%d%d",&edges[i].src,&edges[i].dst,&edges[i].cost);
  46. neighbors[edges[i].src].push_back(i);
  47. neighbors[edges[i].dst].push_back(i);
  48. }
  49. init();
  50. int nrsol=0;
  51. int totalCost=0;
  52. while(N>0)
  53. {
  54. int v=heap[1].vertex;
  55. rmv(1);
  56. if(e[v]!=0)
  57. {
  58. sol[++nrsol]=e[v];
  59. totalCost+=edges[e[v]].cost;
  60. }
  61. for(unsigned int i=0;i<neighbors[v].size();i++)
  62. {
  63. int muchie=neighbors[v][i];
  64. int vec=(v==edges[muchie].src)?edges[muchie].dst:edges[muchie].src;
  65. if(in_heap[vec] && heap[pos[vec]].cost>edges[muchie].cost)
  66. {
  67. heap[pos[vec]].cost=edges[muchie].cost;
  68. e[vec]=muchie;
  69. percolate(pos[vec]);
  70. }
  71. }
  72. }
  73. fprintf(fout,"%d\n%d\n",totalCost,nrsol);
  74. for(int i=1;i<=nrsol;i++)
  75. {
  76. fprintf(fout,"%d %d\n",edges[sol[i]].src,edges[sol[i]].dst);
  77. }
  78. fclose(fin);
  79. fclose(fout);
  80. return 0;
  81. }
  82. int left_son(int nod)
  83. {
  84. return 2*nod;
  85. }
  86. int right_son(int nod)
  87. {
  88. return 2*nod+1;
  89. }
  90. int father(int nod)
  91. {
  92. return nod/2;
  93. }
  94. void add(int vertex,int cost)
  95. {
  96. heap[++N].vertex=vertex;
  97. heap[N].cost=cost;
  98. pos[vertex]=N;
  99. in_heap[vertex]=1;
  100. percolate(N);
  101. }
  102. void rmv(int nod)
  103. {
  104. in_heap[heap[nod].vertex]=0;
  105. swap_nodes(nod,N);
  106. N--;
  107. if(nod>1 && heap[nod].cost<heap[father(nod)].cost)
  108. {
  109. percolate(nod);
  110. }
  111. else
  112. {
  113. sift(nod);
  114. }
  115. }
  116. void sift(int nod)
  117. {
  118. int son;
  119. do
  120. {
  121. son=0;
  122. if(right_son(nod)<=N)
  123. {
  124. if(heap[left_son(nod)].cost<heap[right_son(nod)].cost)
  125. {
  126. son=left_son(nod);
  127. }
  128. else
  129. {
  130. son=right_son(nod);
  131. }
  132. }
  133. else if(left_son(nod)<=N)
  134. {
  135. son=right_son(nod);
  136. }
  137. if(heap[nod].cost<heap[son].cost)
  138. {
  139. son=0;
  140. }
  141. if(son)
  142. {
  143. swap_nodes(nod,son);
  144. nod=son;
  145. }
  146. }while(son);
  147. }
  148. void percolate(int nod)
  149. {
  150. while(nod>1 && heap[nod].cost<heap[father(nod)].cost)
  151. {
  152. swap_nodes(nod,father(nod));
  153. nod=father(nod);
  154. }
  155. }
  156. void swap_nodes(int nod1,int nod2)
  157. {
  158. std::swap(heap[nod1].vertex,heap[nod2].vertex);
  159. std::swap(heap[nod1].cost,heap[nod2].cost);
  160. std::swap(pos[heap[nod1].vertex],pos[heap[nod2].vertex]);
  161. }
  162. void init()
  163. {
  164. for(int i=1;i<=V;i++)
  165. {
  166. add(i,INF);
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement