洛谷 P1850 换教室(概率)

P1850 换教室

题意:略

题解:设dp[i][j][0] 为前i个数中申请j个课,且当前没申请的状态
dp[i][j][1]为前j个数中申请了j个课,且当前申请的状态
状态转移:
dp[i][j][0] = min(dp[i - 1][j][0] + f[c[i - 1]][c[i]], dp[i - 1][j][1] + k[i - 1] * f[d[i - 1]][c[i]] + (1.00 - k[i - 1]) * f[c[i - 1]][c[i]]);
dp[i][j][1] = min(dp[i - 1][j - 1][0] + f[c[i - 1]][c[i]] * (1.00 - k[i]) + f[c[i - 1]][d[i]] * k[i], dp[i - 1][j - 1][1] + (f[c[i - 1]][c[i]] * (1.00 - k[i - 1]) * (1.00 - k[i])) + (f[c[i - 1]][d[i]] * (1.00 - k[i - 1]) * k[i]) + (f[d[i - 1]][c[i]] * k[i - 1] * (1.00 - k[i])) + (f[d[i - 1]][d[i]] * k[i - 1] * k[i]));

其中f[i][j] 为 i 到 j 的最短路

#include<bits/stdc++.h>
using namespace std;
const int N = 2005;
const int M = 305;
int c[N], d[N];
double k[N];
double dp[N][N][2];
double f[M][M];
int main()
{
    int n, m, v, e;
    scanf("%d %d %d %d", &n, &m, &v, &e);
    for(int i = 1; i <= n; i++)
        scanf("%d", &c[i]);
    for(int i = 1; i <= n; i++)
        scanf("%d", &d[i]);
    for(int i = 1; i <= n; i++)
        scanf("%lf", &k[i]);
    for(int i = 1; i <= v; i++)
        for(int j = 1; j <= v; j++)
            f[i][j] = 1e9 + 7;
    for(int i = 1; i <= v; i++)
        f[i][i] = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 0; j <= n; j++)
            dp[i][j][0] = dp[i][j][1] = 1e9 + 7;
    while(e--)
    {
        int a, b;
        double w;
        scanf("%d %d %lf", &a, &b, &w);
        f[a][b] = f[b][a] = min(w,f[a][b]);
    }
    for(int k = 1; k <= v; k++)                 
        for(int i = 1; i <= v; i++)
            for(int j = 1;j <= v; j++)
                f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
    dp[1][0][0] = dp[1][1][1] = 0;
    for(int i = 2; i <= n; i++)
    {
        dp[i][0][0] = dp[i - 1][0][0] + f[c[i - 1]][c[i]];
        for(int j = 1; j <= m; j++)
        {
            dp[i][j][0] = min(dp[i - 1][j][0] + f[c[i - 1]][c[i]], dp[i - 1][j][1] + k[i - 1] * f[d[i - 1]][c[i]] + (1.00 - k[i - 1]) * f[c[i - 1]][c[i]]);
            dp[i][j][1] = min(dp[i - 1][j - 1][0] + f[c[i - 1]][c[i]] * (1.00 - k[i]) + f[c[i - 1]][d[i]] * k[i], dp[i - 1][j - 1][1]
            + (f[c[i - 1]][c[i]] * (1.00 - k[i - 1]) * (1.00 - k[i]))
            + (f[c[i - 1]][d[i]] * (1.00 - k[i - 1]) * k[i])
            + (f[d[i - 1]][c[i]] * k[i - 1] * (1.00 - k[i]))
            + (f[d[i - 1]][d[i]] * k[i - 1] * k[i]));
        }
    }
    double ans = 1e9 + 7;
    for(int i = 0; i <= m; i++)
        ans = min(ans, min(dp[n][i][0], dp[n][i][1]));
    printf("%.2f\n", ans);
    return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页