-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprima.cpp
More file actions
80 lines (69 loc) · 2.21 KB
/
prima.cpp
File metadata and controls
80 lines (69 loc) · 2.21 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
#include "gen.h"
#include <cstdio>
#include <algorithm>
#include <tuple>
#include <vector>
#include <numeric>
#include <queue>
using namespace std;
vid_t *comp;
eid_t *startEid;
struct Que {
weight_t w;
vid_t v;
bool operator<(const Que& other) const {
return w > other.w;
}
};
void addVertex(priority_queue<Que>& que, vid_t v) {
const vid_t cv = comp[v];
eid_t e = startEid[v];
while (e < edgesIds[v + 1] && comp[edges[e].dest] == cv) ++e;
startEid[v] = e;
if (e == edgesIds[v + 1]) return ;
que.push(Que{edges[e].weight, v});
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s input\n", argv[0]);
return 1;
}
int64_t timeRead = -currentNanoTime();
readAll(argv[1]);
timeRead += currentNanoTime();
int64_t timeInit = -currentNanoTime();
comp = static_cast<vid_t*>(malloc(sizeof(vid_t) * vertexCount));
startEid = static_cast<eid_t*>(malloc(sizeof(eid_t) * vertexCount));
iota(comp, comp + vertexCount, 0);
for (vid_t v = 0; v < vertexCount; ++v) {
sort(edges + edgesIds[v], edges + edgesIds[v + 1], EdgeWeightCmp());
startEid[v] = edgesIds[v];
}
timeInit += currentNanoTime();
int64_t timeBuild = -currentNanoTime();
weight_t result = 0;
for (vid_t v = 0; v < vertexCount; ++v) if (comp[v] == v) {
priority_queue<Que> que;
addVertex(que, v);
while (!que.empty()) {
Que best = que.top();
que.pop();
const eid_t e = startEid[best.v];
const vid_t u = edges[e].dest;
if (comp[u] != v) {
comp[u] = v;
result += edges[e].weight;
addVertex(que, u);
}
addVertex(que, best.v);
}
}
timeBuild += currentNanoTime();
printf("%.10lf\n", double(result));
fprintf(stderr, "Read time: %.5lf\n", double(timeRead) / 1e9);
fprintf(stderr, "Init time: %.5lf\n", double(timeInit) / 1e9);
fprintf(stderr, "Bild time: %.5lf\n", double(timeBuild) / 1e9);
fprintf(stderr, "\n");
fprintf(stderr, "%.3lf\n%.3lf\n", double(timeRead + timeInit) / 1e9, double(0 + timeBuild) / 1e9);
return 0;
}