-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathBankerAlgorithm.cpp
66 lines (58 loc) · 1.28 KB
/
BankerAlgorithm.cpp
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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 1e5 + 5;
int n, m;
int available[MAXN];
int allocation[MAXN][MAXN];
int need[MAXN][MAXN];
int request[MAXN];
bool check(int process) {
for (int i = 0; i < m; i++) {
if (need[process][i] < request[i]) {
return false;
}
}
return true;
}
void banker() {
for (int i = 0; i < n; i++) {
if (check(i)) {
for (int j = 0; j < m; j++) {
available[j] += allocation[i][j];
allocation[i][j] = 0;
need[i][j] = 0;
}
}
}
}
int main() {
// read input
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> available[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> allocation[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> need[i][j];
}
}
// simulate resource requests
while (cin >> request[0]) {
for (int i = 1; i < m; i++) {
cin >> request[i];
}
if (check(0)) {
banker();
} else {
cout << "Request cannot be granted." << endl;
}
}
return 0;
}