Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- struct Edge{
- int v,key;
- struct Edge *next;
- };
- struct Top{
- int v_v,value,index,key;
- struct Edge *next;
- } *M,*v,min;
- struct PrioQueue{
- struct Top **heap;
- int count;
- } q;
- int N,E;
- void Heapify(int i,int x);
- void Insert(int v);
- void ExtractMin();
- void DecreaseKey(int x,int key);
- void AddEdge(int a,int b,int key);
- void Edgesfree();
- void Edgefree(struct Edge *a);
- int main(){
- int i,j=0,k,x,y,z,sum=0,d=10000,f=0;
- scanf(" %d %d",&N,&E);
- struct Edge *s;
- M=malloc(sizeof(struct Top)*N);
- q.heap=malloc(sizeof(struct Top*)*N);
- q.count=0;
- for(i=0;i<N;i++){
- M[i].next=NULL;
- M[i].v_v=i;
- M[i].index=-1;
- M[i].key=-1;
- }
- for(i=0;i<E;i++){
- scanf(" %d %d %d",&x,&y,&z);
- AddEdge(x,y,z);
- }
- v=&M[0];
- for(;;){
- v->index=-2;
- s=v->next;
- while(s!=NULL){
- if (M[s->v].index==-1){
- M[s->v].key=s->key;
- M[s->v].value=v->v_v;
- sum+=s->key;
- Insert(s->v);
- }
- else if (M[s->v].index!=-2 && s->key<M[s->v].key){
- M[s->v].value=v->v_v;
- sum-=M[s->v].key-s->key;
- DecreaseKey(M[s->v].index,s->key);
- }
- s=s->next;
- }
- if (q.count==0)
- break;
- ExtractMin();
- v=&M[min.v_v];
- }
- printf("%d\n",sum);
- Edgesfree();
- free(M);
- return 0;
- }
- void Heapify(int i,int x){
- int l,r,j,k=i;;
- struct Top *a;
- for(;;){
- l=2*k+1;r=l+1;j=k;
- if(l<x && q.heap[k]->key>q.heap[l]->key)
- k=l;
- if(r<x && q.heap[k]->key>q.heap[r]->key)
- k=r;
- if(k==j)
- break;
- a=q.heap[k];
- q.heap[k]=q.heap[j];
- q.heap[j]=a;
- q.heap[k]->index=k;
- q.heap[j]->index=j;
- }
- }
- void Insert(int p){
- struct Top *s;
- int i=q.count;
- q.count++;
- q.heap[i]=&M[p];
- while(i>0 && q.heap[(i-1)/2]->key>q.heap[i]->key){
- s=q.heap[(i-1)/2];
- q.heap[(i-1)/2]=q.heap[i];
- q.heap[i]=s;
- q.heap[i]->index=i;
- i=(i-1)/2;
- }
- q.heap[i]->index=i;
- }
- void ExtractMin(){
- struct Top *a;
- min=*q.heap[0];
- q.count--;
- if (q.count>0){
- q.heap[0]=q.heap[q.count];
- q.heap[0]->index=0;
- Heapify(0,q.count);
- }
- }
- void DecreaseKey(int x,int key){
- int i=q.heap[x]->index;
- struct Top *s;
- q.heap[x]->key=key;
- while(i>0 && q.heap[(i-1)/2]->key>key){
- s=q.heap[(i-1)/2];
- q.heap[(i-1)/2]=q.heap[i];
- q.heap[i]=s;
- q.heap[i]->index=i;
- i=(i-1)/2;
- }
- q.heap[i]->index=i;
- }
- void AddEdge(int a,int b,int key){
- struct Edge *s;
- s=M[a].next;
- if(s!=NULL){
- while(s->next!=NULL)
- s=s->next;
- s->next=(struct Edge*)malloc(sizeof(struct Edge));
- s=s->next;
- s->v=b;
- s->key=key;
- s->next=NULL;
- }
- else{
- M[a].next=(struct Edge*)malloc(sizeof(struct Edge));
- M[a].next->v=b;
- M[a].next->key=key;
- M[a].next->next=NULL;
- }
- s=M[b].next;
- if(s!=NULL){
- while(s->next!=NULL)
- s=s->next;
- s->next=(struct Edge*)malloc(sizeof(struct Edge));
- s=s->next;
- s->v=a;
- s->key=key;
- s->next=NULL;
- }
- else{
- M[b].next=(struct Edge*)malloc(sizeof(struct Edge));
- M[b].next->v=a;
- M[b].next->key=key;
- M[b].next->next=NULL;
- }
- }
- void Edgesfree(){
- int i;
- for(i=0;i<N;i++){
- Edgefree(M[i].next);
- }
- }
- void Edgefree(struct Edge *a){
- struct Edge *s;
- s=a;
- if(s!=NULL){
- Edgefree(s->next);
- free(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement