-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.cpp
More file actions
63 lines (58 loc) · 1.48 KB
/
Copy pathprims.cpp
File metadata and controls
63 lines (58 loc) · 1.48 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
#include <bits/stdc++.h>
#define INF INT_MAX
#define NIL -1
using namespace std;
typedef vector<pair<int, int>> graph;
vector<int> prims(graph Graph[], int V)
{
priority_queue<pair<int, int>, graph, greater<pair<int, int>>> min_heap;
vector<int> key(V, INF);
vector<int> parent(V, NIL);
vector<bool> visited(V, false);
int src = 0;
key[src] = 0;
min_heap.push(make_pair(key[src], src));
while (!min_heap.empty())
{
int u = min_heap.top().second;
min_heap.pop();
if (!visited[u])
{
visited[u] = true;
int n = Graph[u].size();
for (int i = 0; i < n; ++i)
{
int v = Graph[u][i].first;
int w = Graph[u][i].second;
if (!visited[v] && key[v] > w)
{
key[v] = w;
min_heap.push(make_pair(key[v], v));
parent[v] = u;
}
}
}
}
return parent;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int V, E;
cin >> V >> E;
graph Graph[V];
for (int i = 0; i < E; ++i)
{
int s, d, w;
cin >> s >> d >> w;
Graph[s].push_back(make_pair(d, w));
Graph[d].push_back(make_pair(s, w));
}
vector<int> parent = prims(Graph, V);
for (int i = 0; i < V; ++i)
cout << i << " - " << parent[i] << endl;
return 0;
}