0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 20:28:48 +00:00

P1850 [NOIP2016 提高组] 换教室

https://www.luogu.com.cn/record/95239669
This commit is contained in:
Baoshuo Ren 2022-11-24 12:11:30 +08:00
parent 61a6f5bd8c
commit de520c15e2
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

92
Luogu/P1850/P1850.cpp Normal file
View File

@ -0,0 +1,92 @@
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2005,
V = 305;
int n, m, v, e, c[N], d[N], g[V][V];
double p[N], f[N][N][2], ans = 1e9;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> v >> e;
memset(g, 0x3f, sizeof(g));
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= v; i++) {
g[i][i] = 0;
}
for (int i = 1, a, b, w; i <= e; i++) {
cin >> a >> b >> w;
g[a][b] = g[b][a] = std::min(g[a][b], w);
}
for (int k = 1; k <= v; k++) {
for (int i = 1; i <= v; i++) {
for (int j = 1; j <= v; j++) {
g[i][j] = std::min(g[i][j], g[i][k] + g[k][j]);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++) {
f[i][j][0] = f[i][j][1] = 1e9;
}
}
f[1][0][0] = f[1][1][1] = 0;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= std::min(m, i); j++) {
f[i][j][0] = std::min(
f[i - 1][j][0]
+ g[c[i - 1]][c[i]],
f[i - 1][j][1]
+ p[i - 1] * g[d[i - 1]][c[i]]
+ (1.0 - p[i - 1]) * g[c[i - 1]][c[i]]);
if (j) {
f[i][j][1] = std::min(
f[i - 1][j - 1][0]
+ p[i] * g[c[i - 1]][d[i]]
+ (1.0 - p[i]) * g[c[i - 1]][c[i]],
f[i - 1][j - 1][1]
+ p[i - 1] * p[i] * g[d[i - 1]][d[i]]
+ (1.0 - p[i - 1]) * p[i] * g[c[i - 1]][d[i]]
+ p[i - 1] * (1.0 - p[i]) * g[d[i - 1]][c[i]]
+ (1.0 - p[i - 1]) * (1.0 - p[i]) * g[c[i - 1]][c[i]]);
}
}
}
for (int i = 0; i <= m; i++) {
ans = std::min({ans, f[n][i][0], f[n][i][1]});
}
cout << std::fixed << std::setprecision(2) << ans << endl;
return 0;
}