-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim'sMST.cpp
More file actions
74 lines (68 loc) · 1.74 KB
/
Copy pathPrim'sMST.cpp
File metadata and controls
74 lines (68 loc) · 1.74 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair < ll,ll > ipair; //not we can't #define it
#define pb push_back
#define mp make_pair
#define ff first
#define vsort(v) sort(v.begin(),v.end())
#define setpr fixed<<setprecision
#define gl(a) getline(cin,a)
#define ss second
#define ld long double
#define cf cout.flush()
#define INF LLONG_MAX
#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void ZZ(const char* name, Arg1&& arg1){std::cerr << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " = " << arg1;
ZZ(comma, args...);
}
#define countd(n) (log10(n)+1)
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
vector < pair < ll,ll > > adj[1002];
bool vis[1002]={0};
ll n;
void PrimMST()
{
int src=0;
priority_queue < ipair ,vector < ipair > , greater < ipair > > pq; //just a syntax for min heap
vector < ll > key(n,INF),parent(n,-1);
key[src]=0;
pq.push(make_pair(0, src));
while(!pq.empty())
{
pair < ll,ll > p=pq.top();
ll u=p.ss; //note second here custom defined
pq.pop();
vis[u]=true;
for(auto i : adj[u])
{
ll v = i.ff;
ll we = i.ss;
if(!vis[v] and key[v] > we) //vis[v] not vis[i]
{
key[v]=we;
parent[v]=u;
pq.push({key[v],v});
}
}
}
for ( ll i = 1; i < n; ++i)
printf("%lli - %lli\n", parent[i], i);
}
int main() {
ll u,v,edges,we;
cin>>n>>edges;
for ( ll i = 0 ; i < edges ; i++ )
{
cin>>u>>v>>we;
adj[u].pb({v,we});
adj[v].pb({u,we});
}
PrimMST();
return 0;
}