-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7C.cpp
More file actions
75 lines (62 loc) · 1.64 KB
/
Copy path7C.cpp
File metadata and controls
75 lines (62 loc) · 1.64 KB
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
#include <iostream>
using namespace std;
int main()
{
int n, i, j, k, row, col, mincost = 0, min;
char op;
cout << "Enter no. of vertices: ";
cin >> n;
int cost[n][n];
int visit[n];
for (i = 0; i < n; i++)
visit[i] = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cost[i][j] = -1;
}
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
cout << "Do you want an edge between " << i + 1 << " and " << j + 1 << ": ";
// use 'i' & 'j' if your vertices start from 0
cin >> op;
if (op == 'y' || op == 'Y')
{
cout << "Enter weight: ";
cin >> cost[i][j];
cost[j][i] = cost[i][j];
}
}
}
visit[0] = 1;
for (k = 0; k < n - 1; k++)
{
min = 999;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (visit[i] == 1 && visit[j] == 0)
{
if (cost[i][j] != -1 && min > cost[i][j])
{
min = cost[i][j];
row = i;
col = j;
}
}
}
}
mincost += min;
visit[col] = 1;
cost[row][col] = cost[col][row] = -1;
cout << row + 1 << "->" << col + 1 << endl;
// use 'row' & 'col' if your vertices start from 0
}
cout << "\nMin. Cost: " << mincost;
return 0;
}