-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1160_network.cpp
More file actions
139 lines (104 loc) · 2.88 KB
/
Copy path1160_network.cpp
File metadata and controls
139 lines (104 loc) · 2.88 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <unordered_map>
#include <forward_list>
#include <queue>
#include <map>
#include <sstream>
using namespace std;
static const int INFINITY = 12000000;
static const int ORPHAN_NODE = -1;
bool compare( pair<int,int> a, pair<int,int> b ) {
return a.first > b.first;
}
class UndirectedGraph {
private:
struct GraphNode {
int key = INFINITY;
int parent = ORPHAN_NODE;
forward_list< pair<int, int> > adjacents;
};
GraphNode* _nodes;
int _nodes_count;
public:
UndirectedGraph( int const nodes_count ) {
_nodes = new GraphNode[ nodes_count ];
_nodes_count = nodes_count;
}
void add_edge( int const node_a, int const node_b, int const weight ) {
_nodes[ node_a ].adjacents.push_front( pair<int,int>(node_b, weight) );
_nodes[ node_b ].adjacents.push_front( pair<int,int>(node_a, weight) );
}
/* Prim algorithm */
void minimal_spanning_tree() {
bool discovered[ _nodes_count ] = {};
_nodes[0].key = 0;
priority_queue<
pair<int,int>,
vector<pair<int,int>>,
decltype(&compare)
> pqueue(&compare);
pqueue.push( pair<int,int> (0, 0) );
while ( !pqueue.empty() ) {
int i = pqueue.top().second;
pqueue.pop();
if ( discovered[i] ) continue;
discovered[i] = true;
for ( auto it = _nodes[i].adjacents.begin(); it != _nodes[i].adjacents.end(); it++ ) {
if ( discovered[it-> first] || it-> second > _nodes[it-> first].key ) continue;
_nodes[it-> first].parent = i;
_nodes[it-> first].key = it-> second;
pqueue.push( pair<int,int> (it-> second, it-> first) );
}
}
}
string traverse_mst() {
stringstream ss;
int max = 0;
for ( int i = 0; i < _nodes_count; i++ ) {
for ( auto it = _nodes[i].adjacents.begin(); it != _nodes[i].adjacents.end(); it++ ) {
if ( _nodes[ it-> first ].parent == i ) {
if ( _nodes[ it->first ].key > max ) max = _nodes[ it-> first ].key;
ss << i + 1 << " " << it-> first + 1 << endl;
}
}
}
cout << max << endl;
return ss.str();
}
};
int read_int () {
bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
return minus ? -result : result;
}
int main() {
int nodes_count = read_int();
int edges_count = read_int();
//cin >> nodes_count >> edges_count;
UndirectedGraph graph( nodes_count );
for ( int i = 0; i < edges_count; i++ ) {
int source = read_int();
int dest = read_int();
int weight = read_int();
//cin >> source >> dest >> weight;
graph.add_edge( source - 1, dest - 1, weight );
}
graph.minimal_spanning_tree();
string out = graph.traverse_mst();
cout << nodes_count - 1 << endl;
cout << out;
return 0;
}