# bzoj 4720 Noip 2016 换教室 概率DP+最短路

Description

Input

Output

Sample Input
3 2 3 3
2 1 2
1 2 1
0.8 0.2 0.5
1 2 5
1 3 3
2 3 1

Sample Output
2.80

f[i][j][1/0]前i个时段，申请了j次换教室，第i个时段申请或不申请的最小期望值。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 305
#define maxn 2005
using namespace std;
int n,m,v,e,c[maxn],d[maxn];
double ans,k[maxn],g[maxv][maxv],f[maxn][maxn][2];
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
void floyd(){
for (int i=1;i<=v;i++) g[i][i]=0;
for (int t=1;t<=v;t++)
for (int i=1;i<=v;i++)
for (int j=1;j<=v;j++)
if (g[i][t]+g[t][j]<g[i][j]) g[i][j]=g[i][t]+g[t][j];
}
int main(){
freopen("4720.in","r",stdin);
freopen("4720.out","w",stdout);
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++) g[i][j]=1<<30;
for (int i=1;i<=e;i++){
if (w<g[x][y]) g[x][y]=g[y][x]=w;
}
floyd();
for(int i=1;i<=n;i++)
for(int j=0;j<=m;j++) f[i][j][0]=f[i][j][1]=1e100;
for (int i=1;i<=n;i++)
for (int j=0;j<=m;j++){
f[i][j][0]=min(f[i-1][j][0]+g[c[i-1]][c[i]],f[i-1][j][1]+(double)g[c[i-1]][c[i]]*(1.0-k[i-1])+(double)g[d[i-1]][c[i]]*k[i-1]);
if (j){
f[i][j][1]=f[i-1][j-1][0]+(double)g[c[i-1]][c[i]]*(1-k[i])+(double)g[c[i-1]][d[i]]*k[i];
f[i][j][1]=min(f[i][j][1],f[i-1][j-1][1]+(double)g[c[i-1]][c[i]]*(1.0-k[i-1])*(1.0-k[i])+(double)g[c[i-1]][d[i]]*(1.0-k[i-1])*k[i]+(double)g[d[i-1]][c[i]]*k[i-1]*(1.0-k[i])+(double)g[d[i-1]][d[i]]*k[i-1]*k[i]);
}
}
ans=1e100;
for (int j=0;j<=m;j++){
ans=min(ans,f[n][j][0]);
ans=min(ans,f[n][j][1]);
}
printf("%.2lf\n",ans);
return 0;
}

04-26 1015

01-10 240

10-19 94

07-25 774

06-12 1782

03-06 35

03-13 504

05-18 25

03-14 734

08-24 331

#### 【BZOJ1415】【codevs1784】聪聪与可可，概率DP

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。