-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
111 lines (101 loc) · 2.25 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
typedef struct weight{
int weight;
int i;
int j;
}Weight;
void sortWeight(struct weight **A,int e){
int i,j;
struct weight *temp;
for(i=0;i<e;i++){
for(j=0;j<e-1;j++){
if(A[j]->weight > A[j+1]->weight){
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
}
int search(int *SET, int i, int j){
if(SET[i]!=SET[j])
return 1;
else
return 0;
}
void UNION(int *SET, int a, int b, int n){
int i;
for(i=0;i<n;i++){
if(SET[i]==b){
SET[i] = a;
}
}
}
int main(){
int n;
printf("Enter n: ");
scanf("%d",&n);
int **A = (int**)malloc(n*sizeof(int*)),i,j,e,SET[n];
Weight **W = (Weight **)malloc(100*sizeof(Weight*));
for(i=0;i<100;i++){
W[i] = (Weight*)malloc(sizeof(Weight));
}
//W[9]
for(i=0;i<n;i++){
A[i] = (int*)malloc(n*sizeof(int));
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
A[i][j] = 0;
}
SET[i] = i;
}
e=0;
while(1){
printf("Enter weight,from,to (-1 -1 -1 to break) to exit:\n");
int wgt;
scanf("%d %d %d",&wgt,&i,&j);
if(wgt == -1 && i == -1 && j == -1){
break;
}
else{
A[i][j] = wgt;
printf("%d",e);
W[e]->weight = wgt;
printf("%d",e);
W[e]->i = i;
printf("%d",e);
W[e]->j = j;
printf("%d",e);
e++;
printf("%d\n",e);
}
}
sortWeight(W,e);
for(i=0;i<e;i++){
printf("(%d,%d,%d) ", W[i]->weight, W[i]->i, W[i]->j);
}
int sum=0;
printf("\n Adjacency matrix is\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",A[i][j]);
}
//SET[i] = 0;
printf("\n");
}
printf("Adj matrix done\n");
int k;
for(k=0;k<e;k++){
if(search(SET,W[k]->i,W[k]->j)){
int a = SET[W[k]->i];
int b = SET[W[k]->j];
UNION(SET,a,b,n);
sum+=W[k]->weight;
printf("%d %d %d \n",W[k]->weight,W[k]->i,W[k]->j);
}
}
printf("%d",sum);
return 0;*/
}