P1850 [NOIP2016 提高组] 换教室

Problem

Solution

\(dp[i][j][0/1]\)表示在前\(i\)节课中,申请\(j\)节,同时第\(i\)节课申不申请的期望值。
首先需要Floyd预处理出各个教室之间最短路。
不难发现\(dp[i][..][..]\)一定由\(dp[i - 1][..][..]\)转移来。
分类讨论:

  • \(i\)不申请
    • \(i - 1\)不申请
      \(dp[i][j][0] = dp[i - 1][j][0] + w[c[i - 1]][c[i]]\)
    • \(i - 1\)申请
      \(dp[i][j][0] = dp[i - 1][j][1] + w[d[i - 1]][c[i]] \cdot k[i - 1] + w[c[i - 1]][c[i]] \cdot (1 - k[i - 1])\)
  • \(i\)申请
    • \(i - 1\)不申请
      \(dp[i][j][1] = dp[i - 1][j - 1][0] + w[c[i - 1]][d[i]] \cdot k[i] + w[c[i - 1]][c[i]] \cdot (1 - k[i])\)
    • \(i - 1\)申请
      \(dp[i][j][1] = dp[i - 1][j - 1][1] + w[d[i - 1]][d[i]] \cdot k[i - 1] \cdot k[i] + w[d[i - 1]][c[i]] \cdot k[i - 1] \cdot (1 - k[i]) + w[c[i - 1]][d[i]] \cdot (1 - k[i - 1]) \cdot k[i] + w[c[i - 1]][c[i]] \cdot (1 - k[i - 1]) \cdot (1 - k[i])\)
# include <bits/stdc++.h>
using namespace std;
int n,m,V,E;
int w[305][305];
int c[2005],d[2005];
double K[2005];
double dp[2005][2005][2];
const int inf = 0x3f3f3f3f;
int main(void)
{
    // freopen("P1850_2.in","r",stdin);
    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]);
    memset(w,0x3f3f3f,sizeof(w));
    for(int i = 1; i <= V; i++) w[i][i] = 0;
    for(int i = 1; i <= E; i++)
    {
        int a,b,_w;
        scanf("%d%d%d",&a,&b,&_w);
        w[a][b] = w[b][a] = min(w[a][b],_w);
    }
    for(int k = 1; k <= V; k++)
    {
        for(int i = 1; i <= V; i++)
        {
            for(int j = 1; j <= V; j++) 
            {
                if(i == j) continue;
                w[i][j] = min(w[i][j],w[i][k] + w[k][j]);
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j <= m; j++)
        {
            dp[i][j][0] = dp[i][j][1] = (double)inf;
        }
    }
    dp[1][0][0] = dp[1][1][1] = 0;
    for(int i = 2; i <= n; i++)
    {
        for(int j = 0; j <= min(i,m); j++)
        {
            dp[i][j][0] = min(dp[i][j][0],dp[i - 1][j][0] + (double)w[c[i - 1]][c[i]]);
            dp[i][j][0] = min(dp[i][j][0],dp[i - 1][j][1] + (double)w[d[i - 1]][c[i]] * K[i - 1] + (double)w[c[i - 1]][c[i]] * (1 - K[i - 1]));
            if(j == 0) continue;
            dp[i][j][1] = min(dp[i][j][1],dp[i - 1][j - 1][0] + (double)w[c[i - 1]][d[i]] * K[i] + (double)w[c[i - 1]][c[i]] * (1 - K[i]));
            dp[i][j][1] = min(dp[i][j][1],dp[i - 1][j - 1][1] + (double)w[d[i - 1]][d[i]] * K[i - 1] * K[i] + (double)w[d[i - 1]][c[i]] * K[i - 1] * (1 - K[i]) + (double)w[c[i - 1]][d[i]] * (1 - K[i - 1]) * K[i] + (double)w[c[i - 1]][c[i]] * (1 - K[i - 1]) * (1 - K[i]));
        }
    }
    double ans = (double)inf;
    for(int i = 0; i <= m; i++)
    {
        for(int j = 0; j <= 1; j++)
        {
            if(j == 1 && i == 0) continue;
            ans = min(ans,dp[n][i][j]);
        }
    }
    printf("%.2lf\n",ans);
    return 0;
}
posted @ 2021-08-10 23:21  luyiming123  阅读(42)  评论(0编辑  收藏  举报