Luogu P1850 换教室

题目描述

https://www.luogu.com.cn/problem/P1850

Solution

假装是期望 $dp$ 的简单 $dp$

其实我们只需要记录上次有没有换,那么就可以直接 $dp$ 了

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
#include <iostream>
#include <cstdio>
#include <iomanip>
#define maxn 2010
#define maxm 310
#define INF 1000000000
using namespace std;

int n, m, k, e;

int a[maxn], b[maxn];

double p[maxn];

int g[maxm][maxm];
void floyd(int n) {
for (int i = 1; i <= n; ++i) g[i][i] = 0;
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (i == j || i == k || j == k) continue;
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}

double f[maxn][maxn][2];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> m >> k >> e;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
for (int i = 1; i <= n; ++i) cin >> p[i];
fill(g[0], g[0] + maxm * maxm, INF);
for (int i = 1; i <= e; ++i) {
int x, y, z; cin >> x >> y >> z;
g[y][x] = g[x][y] = min(g[x][y], z);
} floyd(k); fill(f[0][0], f[0][0] + maxn * maxn * 2, INF); f[1][1][1] = f[1][0][0] = 0;
for (int i = 2; i <= n; ++i)
for (int j = 0; j <= min(i, m); ++j) {
f[i][j][0] = min(f[i - 1][j][0] + g[a[i]][a[i - 1]],
f[i - 1][j][1] + g[a[i]][a[i - 1]] * (1 - p[i - 1]) + g[a[i]][b[i - 1]] * p[i - 1]);
if (!j) continue;
f[i][j][1] = min(f[i - 1][j - 1][0] + g[b[i]][a[i - 1]] * p[i] + g[a[i]][a[i - 1]] * (1 - p[i]),
f[i - 1][j - 1][1] +
g[b[i]][a[i - 1]] * p[i] * (1 - p[i - 1]) +
g[b[i]][b[i - 1]] * p[i] * p[i - 1] +
g[a[i]][a[i - 1]] * (1 - p[i]) * (1 - p[i - 1]) +
g[a[i]][b[i - 1]] * (1 - p[i]) * p[i - 1]);
}
double ans = INF;
for (int i = 0; i <= m; ++i) ans = min(ans, min(f[n][i][0], f[n][i][1]));
cout << fixed << setprecision(2) << ans << "\n";
return 0;
}