1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include<stdio.h> #include<stdlib.h>
#define MaxVer 1005 #define Inf 65535
typedef struct GNode* PtrToGNode; struct GNode { int V; int E; int G[MaxVer][MaxVer]; }; typedef PtrToGNode MGraph; int collected[MaxVer] = {0}; int S[MaxVer]; int VisitedE[MaxVer][MaxVer] = {0};
MGraph creatGraph(int V,int E) { int i,V1,V2,D,j; MGraph Graph; Graph = (MGraph)malloc(sizeof(struct GNode)); Graph->V = V; Graph->E = E; for(i = 0;i < V;i++) for(j = 0;j < V;j++) { if(i == j) { Graph->G[i][j] = 0; S[i] = -1; } else Graph->G[i][j] = Inf; } for(i = 0;i < Graph->E;i++) { scanf("%d %d %d\n",&V1,&V2,&D); Graph->G[V1-1][V2-1] = Graph->G[V2-1][V1-1] = D; } return Graph; } int findRoot(int V) { if(S[V] < 0) return V; else return S[V] = findRoot(S[V]); } void Union(int Root1,int Root2) { if(S[Root1] < S[Root2]) { S[Root1] += S[Root2]; S[Root2] = Root1; } else { S[Root2] += S[Root1]; S[Root1] = Root2; } }
void Kruskal(MGraph Graph) { int MinEdge,sum; int V1,V2,p1,p2; int i,j; sum = 0; int num = Graph->V; while(num > 1) { MinEdge = Inf; for(i=0;i<Graph->E;i++) { for(j=i;j<Graph->E;j++) { if((Graph->G[i][j] < MinEdge)&&(VisitedE[i][j] == 0)) { MinEdge = Graph->G[i][j]; V1 = i,V2 = j; } } } if(MinEdge == Inf)break; VisitedE[V1][V2] = 1; VisitedE[V2][V1] = 1; p1 = findRoot(V1); p2 = findRoot(V2); if(p1 != p2){ sum += MinEdge; num--; Union(V1,V2); } } if(num > 1) { printf("-1\n"); return; } printf("%d\n",sum); }
int main(){ int V,E; scanf("%d %d\n",&V,&E); MGraph Graph; Graph = creatGraph(V,E); Kruskal(Graph); return 0; }
|