-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLista6B.cpp
More file actions
182 lines (151 loc) · 3.73 KB
/
Lista6B.cpp
File metadata and controls
182 lines (151 loc) · 3.73 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <vector>
#include <limits>
#include <iterator>
#include <queue>
#include <stack>
#define endl "\n"
#define UNVISITED 0
#define VISITED 1
using namespace std;
typedef struct g{
vector<pair<int, int>> *l; // adjacency list
int numEdge; // number of edges
int n; // number of vertices
int *Mark; // auxiliary marking array
int *D; // distance from a vertex to all the others
int **Dm; // matrix of distances
} G;
G* create_graph(const int n);
int n(G* g);
int e(G* g);
int first(G* g, int v);
int next(G* g, int v, vector<pair<int, int>>::iterator &it);
int weight(G* g, vector<pair<int, int>>::iterator it);
int max(G* g);
void graphTraverse(G* g, int v);
void Prim(G* g);
void setEdge(G* g, int i, int j, int w);
void setMark(G* g, int v, int val);
int getMark(G* g, int v);
int main(void) {
int n = -1, m = -1;
do {
cin >> n >> m;
if(n == 0 && m == 0) {
return 0;
}
G* g = create_graph(n);
for(int i = 0; i < m; i++) {
int x, y, w;
cin >> x >> y >> w;
setEdge(g, x, y, w);
}
graphTraverse(g, 0);
int tmp = max(g);
if(tmp < numeric_limits<int>::max()) {
cout << tmp << endl;
}
else {
cout << "IMPOSSIBLE" << endl;
}
} while(!(n == 0 && m == 0));
return 0;
}
G* create_graph(int n) {
G* g = new G;
g->n = n;
g->Mark = new int[n];
g->D = new int[n];
g->Dm = new int*[n];
g->l = new vector<pair<int, int>>[n];
g->numEdge = 0;
return g;
}
int n(G* g) {
return g->n;
}
int e(G* g) {
return g->numEdge;
}
int first(G* g, int v) {
if(g->l[v].empty()) {
return n(g);
}
return g->l[v].front().first;
}
int next(G* g, int v, vector<pair<int, int>>::iterator &it) {
it++;
if(it == g->l[v].end()) {
return n(g);
}
else {
return (*it).first;
}
}
int weight(G* g, vector<pair<int, int>>::iterator it) {
return (*it).second;
}
int max(G* g) {
int max = 0;
for(int i = 0; i < g->n; i++) {
if(g->D[i] > max) {
max = g->D[i];
}
}
return max;
}
void graphTraverse(G* g, int v) {
for(int i = 0; i <= (n(g)-1); i++) {
setMark(g, i, UNVISITED);
}
if(getMark(g, v) == UNVISITED) {
Prim(g);
}
}
void Prim(G* g) {
int* V = new int[g->n];
int p, v;
vector<pair<int, int>>::iterator it;
priority_queue<pair<int, pair<int,int>>, vector<pair<int, pair<int,int>>>, greater<pair<int, pair<int,int>>>> H;
for(int i = 0; i <= (n(g)-1); i++) {
g->D[i] = numeric_limits<int>::max();
V[i] = -1;
setMark(g, i, UNVISITED);
}
H.push(make_pair(0, make_pair(0, 0)));
g->D[0] = 0;
for(int i = 0; i <= (n(g)-1); i++) {
do {
if(H.empty()) {
return;
}
pair<int, pair<int, int>> tmp = H.top();
H.pop();
p = tmp.second.first;
v = tmp.second.second;
} while(!(getMark(g, v) == UNVISITED));
setMark(g, v, VISITED);
V[v] = p;
int w = first(g, v);
it = g->l[v].begin();
while(w < n(g)) {
if(getMark(g, w) != VISITED && g->D[w] > weight(g, it)) {
g->D[w] = weight(g, it);
H.push(make_pair(g->D[w], make_pair(v, w)));
}
w = next(g, v, it);
}
}
}
void setEdge(G* g, int i, int j, int w) {
g->numEdge++;
g->l[i].push_back(make_pair(j, w));
g->l[j].push_back(make_pair(i, w)); // only for undirected graphs
}
void setMark(G* g, int v, int val) {
g->Mark[v] = val;
}
int getMark(G* g, int v) {
return g->Mark[v];
}