Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. struct Edge{
  6. int v,key;
  7. struct Edge *next;
  8. };
  9. struct Top{
  10. int v_v,value,index,key;
  11. struct Edge *next;
  12. } *M,*v,min;
  13. struct PrioQueue{
  14. struct Top **heap;
  15. int count;
  16. } q;
  17. int N,E;
  18. void Heapify(int i,int x);
  19. void Insert(int v);
  20. void ExtractMin();
  21. void DecreaseKey(int x,int key);
  22. void AddEdge(int a,int b,int key);
  23. void Edgesfree();
  24. void Edgefree(struct Edge *a);
  25. int main(){
  26. int i,j=0,k,x,y,z,sum=0,d=10000,f=0;
  27. scanf(" %d %d",&N,&E);
  28. struct Edge *s;
  29. M=malloc(sizeof(struct Top)*N);
  30. q.heap=malloc(sizeof(struct Top*)*N);
  31. q.count=0;
  32. for(i=0;i<N;i++){
  33. M[i].next=NULL;
  34. M[i].v_v=i;
  35. M[i].index=-1;
  36. M[i].key=-1;
  37. }
  38. for(i=0;i<E;i++){
  39. scanf(" %d %d %d",&x,&y,&z);
  40. AddEdge(x,y,z);
  41. }
  42. v=&M[0];
  43. for(;;){
  44. v->index=-2;
  45. s=v->next;
  46. while(s!=NULL){
  47. if (M[s->v].index==-1){
  48. M[s->v].key=s->key;
  49. M[s->v].value=v->v_v;
  50. sum+=s->key;
  51. Insert(s->v);
  52. }
  53. else if (M[s->v].index!=-2 && s->key<M[s->v].key){
  54. M[s->v].value=v->v_v;
  55. sum-=M[s->v].key-s->key;
  56. DecreaseKey(M[s->v].index,s->key);
  57. }
  58. s=s->next;
  59. }
  60. if (q.count==0)
  61. break;
  62. ExtractMin();
  63. v=&M[min.v_v];
  64. }
  65. printf("%d\n",sum);
  66. Edgesfree();
  67. free(M);
  68. return 0;
  69. }
  70.  
  71. void Heapify(int i,int x){
  72. int l,r,j,k=i;;
  73. struct Top *a;
  74. for(;;){
  75. l=2*k+1;r=l+1;j=k;
  76. if(l<x && q.heap[k]->key>q.heap[l]->key)
  77. k=l;
  78. if(r<x && q.heap[k]->key>q.heap[r]->key)
  79. k=r;
  80. if(k==j)
  81. break;
  82. a=q.heap[k];
  83. q.heap[k]=q.heap[j];
  84. q.heap[j]=a;
  85. q.heap[k]->index=k;
  86. q.heap[j]->index=j;
  87. }
  88. }
  89.  
  90. void Insert(int p){
  91. struct Top *s;
  92. int i=q.count;
  93. q.count++;
  94. q.heap[i]=&M[p];
  95. while(i>0 && q.heap[(i-1)/2]->key>q.heap[i]->key){
  96. s=q.heap[(i-1)/2];
  97. q.heap[(i-1)/2]=q.heap[i];
  98. q.heap[i]=s;
  99. q.heap[i]->index=i;
  100. i=(i-1)/2;
  101. }
  102. q.heap[i]->index=i;
  103. }
  104. void ExtractMin(){
  105. struct Top *a;
  106. min=*q.heap[0];
  107. q.count--;
  108. if (q.count>0){
  109. q.heap[0]=q.heap[q.count];
  110. q.heap[0]->index=0;
  111. Heapify(0,q.count);
  112. }
  113. }
  114.  
  115. void DecreaseKey(int x,int key){
  116. int i=q.heap[x]->index;
  117. struct Top *s;
  118. q.heap[x]->key=key;
  119. while(i>0 && q.heap[(i-1)/2]->key>key){
  120. s=q.heap[(i-1)/2];
  121. q.heap[(i-1)/2]=q.heap[i];
  122. q.heap[i]=s;
  123. q.heap[i]->index=i;
  124. i=(i-1)/2;
  125. }
  126. q.heap[i]->index=i;
  127. }
  128.  
  129. void AddEdge(int a,int b,int key){
  130. struct Edge *s;
  131. s=M[a].next;
  132. if(s!=NULL){
  133. while(s->next!=NULL)
  134. s=s->next;
  135. s->next=(struct Edge*)malloc(sizeof(struct Edge));
  136. s=s->next;
  137. s->v=b;
  138. s->key=key;
  139. s->next=NULL;
  140. }
  141. else{
  142. M[a].next=(struct Edge*)malloc(sizeof(struct Edge));
  143. M[a].next->v=b;
  144. M[a].next->key=key;
  145. M[a].next->next=NULL;
  146. }
  147. s=M[b].next;
  148. if(s!=NULL){
  149. while(s->next!=NULL)
  150. s=s->next;
  151. s->next=(struct Edge*)malloc(sizeof(struct Edge));
  152. s=s->next;
  153. s->v=a;
  154. s->key=key;
  155. s->next=NULL;
  156. }
  157. else{
  158. M[b].next=(struct Edge*)malloc(sizeof(struct Edge));
  159. M[b].next->v=a;
  160. M[b].next->key=key;
  161. M[b].next->next=NULL;
  162. }
  163. }
  164.  
  165. void Edgesfree(){
  166. int i;
  167. for(i=0;i<N;i++){
  168. Edgefree(M[i].next);
  169. }
  170.  
  171. }
  172.  
  173. void Edgefree(struct Edge *a){
  174. struct Edge *s;
  175. s=a;
  176. if(s!=NULL){
  177. Edgefree(s->next);
  178. free(s);
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement