-
Notifications
You must be signed in to change notification settings - Fork 2
/
6.prims.cpp
65 lines (58 loc) · 1.27 KB
/
6.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
#include<bits/stdc++.h>
using namespace std;
vector<int>adj[100];
vector< int > cost[100];
int node[100];
void bfs(int s, int n)
{
for(int i = 1; i <= n; i++)
{
node[i] = 0;
}
vector < int > q;
q.push_back(s);
node[s] = 1;
for(int i = 1; i <= n-1; i++)
{
int p = 100000, y, z;
for(int j = 0; j < q.size(); j++)
{
for(int k = 0; k < adj[q[j]].size(); k++)
{
if(node[adj[q[j]][k]]==0)
{
if(cost[q[j]][k]<=p)
{
p = cost[q[j]][k];
y = q[j];
z = adj[q[j]][k];
}
}
}
}
cout << p << " " << y << " " << z << endl;
q.push_back(z);
node[z] = 1;
}
}
int main()
{
cout << "Enter the node and edges number\n";
int n, e;
cin>> n >> e;
cout << "Enter node1 node2 and weight \n";
for (int i = 0; i < e; i++)
{
int a, b, w;
cin >> a >> b >> w;
adj[a].push_back(b);
adj[b].push_back(a);
cost[a].push_back(w);
cost[b].push_back(w);
}
cout << "enter source\n";
int s;
cin >> s;
bfs(s,n);
return 0;
}