-
Notifications
You must be signed in to change notification settings - Fork 2
/
l_ewg.h
160 lines (134 loc) · 2.93 KB
/
l_ewg.h
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
#ifndef _L_EWG_H
#define _L_EWG_H
#include "l.h"
#include "l_edge.h"
struct EdgeWeightedGraph {
int V;
int E;
vector<vector<Edge*> > adj;
EdgeWeightedGraph(const char* filename) {
FILE *fp = fopen(filename, "r");
assert(fp);
fscanf(fp, "%d", &V);
fscanf(fp, "%d", &E);
for (int v = 0; v < V; ++v) {
adj.push_back(vector<Edge*>());
}
for (int e = 0; e < E; e++) {
int v;
int w;
double weight;
fscanf(fp, "%d %d %lf", &v, &w, &weight);
addEdge(new Edge(v, w, weight));
}
}
void addEdge(Edge* e) {
int v = e->v;
int w = e->w;
adj[v].push_back(e);
adj[w].push_back(e);
}
void delete_ewg() {
//
}
};
void p_ewg(EdgeWeightedGraph &g) {
int i = 0;
for (auto v : g.adj) {
cout << i++ << ": ";
for (auto e : v) {
cout << *e << "; ";
}
cout << endl;
}
cout << "print EdgeWeightedGraph end" << endl;
}
class PQ_COMP {
public:
bool operator()(Edge* &lhs, Edge* &rhs) {
return *lhs > *rhs;
}
};
class LazyPrimeMST {
public:
vector<bool> marked;
vector<Edge*> mst;
priority_queue<Edge*, vector<Edge*>, PQ_COMP> pq;
LazyPrimeMST(EdgeWeightedGraph &G) {
marked = vector<bool>(G.V);
for (auto i : marked) i = false;
visit(G, 0);
while (!pq.empty()) {
auto e = pq.top();
pq.pop();
int v = e->v;
int w = e->w;
if (marked[v] && marked[w]) continue;
mst.push_back(e);
if (!marked[v]) visit(G, v);
if (!marked[w]) visit(G, w);
}
}
void visit(EdgeWeightedGraph &G, int v) {
marked[v] = true;
for (auto e : G.adj[v])
if (!marked[e->other(v)]) pq.push(e);
}
};
class PrimeMST_COMP {
public:
bool operator()(const pair<int, double> &lhs, const pair<int, double> &rhs) const{
return lhs.second > rhs.second;
}
};
class PrimeMST {
public:
vector<double> m_distTo;
vector<bool> m_marked;
vector<Edge*> m_edgeTo;
priority_queue<pair<int, double>, vector<pair<int, double> >, PrimeMST_COMP > pq;
void relax(EdgeWeightedGraph* g, int v) {
m_marked[v] = true;
for (auto e : g->adj[v]) {
int w = e->other(v);
if (m_marked[w]) continue;
if (m_distTo[w] > e->weight) {
m_distTo[w] = e->weight;
m_edgeTo[w] = e;
pq.push(make_pair(w, m_distTo[w]));
}
}
}
public:
PrimeMST(EdgeWeightedGraph* g) {
for (int v = 0; v < g->V; ++v) {
m_distTo.push_back(numeric_limits<double>::max());
m_marked.push_back(false);
m_edgeTo.push_back(NULL);
}
int s = 0;
m_distTo[s] = 0.0;
pq.push(make_pair(s, m_distTo[s]));
while (!pq.empty()) {
int v = pq.top().first;
pq.pop();
relax(g, v);
}
}
~PrimeMST(){}
vector<Edge* > edges() {
vector<Edge*> result;
for (auto e : m_edgeTo) {
if (e) result.push_back(e);
}
return result;
}
double weight() {
double result(0.0);
for (auto e : edges()) {
result += e->weight;
}
return result;
}
};
#endif // !_L_EWG_H