-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock-bfs.cpp
More file actions
93 lines (72 loc) · 1.85 KB
/
lock-bfs.cpp
File metadata and controls
93 lines (72 loc) · 1.85 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
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#include <semaphore.h>
int N, M;
vector<vector<int>> adj;
vector<int> dist;
int num_t;
vector<int> ops;
queue<int> q;
sem_t mtx;
void lock_bfs(int tid) {
int dpt = 0;
while(1) {
sem_wait(&mtx);
if(q.empty()) {
sem_post(&mtx);
break;
}
int u = q.front(), d = dist[u];
q.pop();
sem_post(&mtx);
for(auto v: adj[u]) {
ops[tid]++;
if(dist[v] == -1) {
dist[v] = d + 1;
sem_wait(&mtx);
q.push(v);
sem_post(&mtx);
}
}
}
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int i, j;
FILE* f_in = fopen(argv[1], "r");
fscanf(f_in, "%d %d %d", &N, &M, &num_t);
adj.resize(N);
dist.resize(N, -1);
ops.resize(num_t, 0);
for(i=0; i<M; i++) {
int x, y;
fscanf(f_in, "%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
fclose(f_in);
dist[0] = 0;
q.push(0);
sem_init(&mtx,0,1);
high_resolution_clock::time_point t1 = high_resolution_clock::now();
vector<thread> th;
for(i=0; i<num_t; i++) {
th.push_back(thread(lock_bfs, i));
}
for(i=0; i<num_t; i++) {
th[i].join();
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(t2 - t1).count();
FILE* f_out = fopen("lock-out.txt", "w");
for(auto d: dist) {
fprintf(f_out, "%d\n", d);
}
fprintf(f_out, "\n");
fprintf(f_out, "%d\n", *max_element(dist.begin(), dist.end()));
fclose(f_out);
for(auto x: ops) cout << x << " "; cout << "\n";
cout << duration << "\n";
return 0;
}