forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
59 lines (52 loc) · 1.45 KB
/
B.cpp
File metadata and controls
59 lines (52 loc) · 1.45 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
#include <bits/stdc++.h>
using namespace std;
const int oo = 1 << 30;
int n, m, w[11], t[11], s[11], c[1111], D[1111], d[1111];
int f[1010][1 << 10], done[1010][1 << 10];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> w[i] >> t[i] >> s[i];
for (int i = 0; i < m; i++)
cin >> c[i] >> D[i] >> d[i];
priority_queue < pair<int, pair<int,int> > > q;
for (int i = 0; i <= m; i++)
for (int mask = 0; mask < 1 << n; mask++)
f[i][mask] = oo;
f[0][0] = 0;
q.push({0, {0, 0}});
while (!q.empty())
{
auto u = q.top();
q.pop();
int i = u.second.first, mask = u.second.second;
if (done[i][mask]++) continue;
if (i == m && !mask)
{
cout << f[i][mask] << endl;
break;
}
// person j changes place
for (int j = 0; j < n; j++)
if (f[i][mask ^ 1 << j] > f[i][mask] + s[j])
{
f[i][mask ^ 1 << j] = f[i][mask] + s[j];
q.push({-f[i][mask ^ 1 << j], {i, mask ^ 1 << j}});
}
if (!mask) continue;
// go to next riffle
int weight = 0, walk = 0;
for (int j = 0; j < n; j++)
if (mask >> j & 1) weight += w[j];
else walk = max(walk, t[j]);
int cost = max(walk, weight > c[i] ? D[i] : d[i]);
if (f[i + 1][mask] > f[i][mask] + cost)
{
f[i + 1][mask] = f[i][mask] + cost;
q.push({-f[i + 1][mask], {i + 1, mask}});
}
}
}