-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmymintree.c
156 lines (119 loc) · 2.33 KB
/
mymintree.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#include<string.h>
#define MaxvertexNum 100
#define MAX 100000
#define VNUM 10+1
#define MAXCOST 65535
typedef char VertexType;
typedef int EdgeType;
typedef struct MGraph
{
VertexType vexs[MaxvertexNum];
EdgeType edges[MaxvertexNum][MaxvertexNum];
int n, e;
}MGraph;
MGraph * CreateMGraph()
{
int i, j, k = 0, minvalue,minid, w, flag;
int lowcost[VNUM];/*Ȩֵ*/
int adjecent[VNUM] = { 0 };
int sumweight = 0;
int closet[MaxvertexNum];/*×îСÉú³ÉÊ÷½áµã*/
char va[MaxvertexNum], vb[MaxvertexNum];
MGraph *G = (MGraph *)malloc(sizeof(MGraph));
printf("please input data of vertex and edge num with blank \n ");
scanf_s("%d %d", &G->n, &G->e);
//for (int d = 0; d < G->n; d++)
//{
// scanf_s("%s",&G->vexs[d]);
//}
for (i = 0; i < G->n; i++)
{
for (j = 0; j < G->n; j++)
{
G->edges[i][j] = MAXCOST;
}
}
//for (k = 0; k < G->e; k++)
//{
// printf("please input data of v-e with weight\n ");
// scanf_s("%d%d%d", &i, &j, &w);
///* i = LocateVex(G, va);
// j= LocateVex(G, vb);*/
// G->edges[i][j] = w;
// G->edges[j][i] = w;
//}
G->edges[0][1] = 6;
G->edges[1][0] = 6;
G->edges[0][2] = 1;
G->edges[2][0] = 1;
G->edges[0][3] = 5;
G->edges[3][0] = 5;
G->edges[1][2] = 5;
G->edges[2][1] = 5;
G->edges[1][4] = 3;
G->edges[4][1] = 3;
G->edges[2][3] = 5;
G->edges[3][2] = 5;
G->edges[2][4] = 6;
G->edges[4][2] = 6;
G->edges[2][5] = 4;
G->edges[5][2] = 4;
G->edges[3][5] = 2;
G->edges[5][3] = 2;
G->edges[4][5] = 6;
G->edges[5][4] = 6;
int mst[MAX];
int min,sum = 0;
for (i = 1; i <G->n; i++)
{
lowcost[i] = G->edges[0][i];
mst[i] = 0;
}
mst[0] = 1;
for (i = 1; i <G->n; i++)
{
min = MAXCOST;
minid = 0;
for (j = 1; j <G->n; j++)
{
if (lowcost[j] < min && lowcost[j] != 0)
{
min = lowcost[j];
minid = j;
}
}
printf("v%d-%d=%d\n", mst[minid], minid, min);
sum += min;
lowcost[minid] = 0;
for (j = 1; j < G->n; j++)
{
if (G->edges[minid][j] < lowcost[j])
{
lowcost[j] = G->edges[minid][j];
mst[j] = minid;
}
}
}
return sum;
return G;
}
void Show(MGraph *G)
{
for (int i = 0; i < G->n; i++)
{
for (int j = 0; j < G->n; j++)
{
printf("%d\t", G->edges[i][j]);
}
printf("\n");
}
printf("\n");
}
void main()
{
MGraph * M = CreateMGraph();
system("pause");
}