-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprims.cpp
69 lines (57 loc) · 1.44 KB
/
prims.cpp
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
#include<iostream>
#define I 32767
using namespace std;
int cost [][8]=
{{I, I, I, I, I, I, I, I},
{I, I, 25, I, I, I, 5, I},
{I, 25, I, 12, I, I, I, 10},
{I, I, 12, I, 8, I, I, I},
{I, I, I, 8, I, 16, I, 14},
{I, I, I, I, 16, I, 20, 18},
{I, 5, I, I, I, 20, I, I},
{I, I, 10, I, 14, 18, I, I}};
int near[8] = {I, I, I, I, I, I, I, I};
int t[2][6];
int main(){
int i,j,k,u,v,n=7,min=1;
for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
if(cost[i][j]<min){
min=cost[i][j];
u=i;
v=j;
}
}
}
t[0][0]=u;
t[1][0]=v;
near[u]=near[v]=0;
for(i=1;i<=n;i++){
if(near[i]!=0){
if(cost[i][u]<cost[i][v])
near[i]=u;
else
near[i]=v;
}
}
for(i=1;i<n-1;i++){
min=I;
for(j=1;j<=n;j++){
if(near[j]!=0 && cost[j][near[j]]<min){
min = cost[j][near[j]];
k=j;
}
}
t[0][i]=k;
t[1][i]=near[k];
near[k]=0;
for(j=1;j<=n;j++){
if(near[j]!=0 && cost[j][k]< cost[j][near[j]])
near[j]=k;
}
}
for(i=0;i<n-1;i++){
cout<<"("<<t[0][i]<<","<<t[1][i]<<")"<<endl;
}
return 0;
}