题解:概率,f[i][j][2]表示到第i天一共申请了j次,第i天的课程是否申请的期望最短路
考场上这题得了0分QWQ
问题:对期望和概率的理解不够深
让保留2位小数我TM用了cout,WA了几发
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=2009; const int oo=1000000000; int n,m,v,e; double k[maxn]; int a[maxn],b[maxn]; int d[maxn][maxn]; double f[maxn][maxn][2]; double ans=oo*1.0; int main(){ scanf("%d%d%d%d",&n,&m,&v,&e); for(int i=1;i<=n;++i)scanf("%d",&a[i]); for(int i=1;i<=n;++i)scanf("%d",&b[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){ d[i][j]=oo; } } for(int i=1;i<=v;++i)d[i][i]=0; while(e--){ int x,y,z; scanf("%d%d%d",&x,&y,&z); d[x][y]=min(d[x][y],z); d[y][x]=min(d[y][x],z); } for(int p=1;p<=v;++p){ for(int i=1;i<=v;++i){ for(int j=1;j<=v;++j){ d[i][j]=min(d[i][j],d[i][p]+d[p][j]); } } } for(int i=1;i<=n;++i){ for(int j=0;j<=m;++j){ f[i][j][0]=f[i][j][1]=oo*1.0; } } f[1][1][1]=0; f[1][0][0]=0; for(int i=2;i<=n;++i){ for(int j=0;j<=m;++j){ f[i][j][0]=min(f[i-1][j][0]+d[a[i-1]][a[i]],f[i-1][j][1]+k[i-1]*d[b[i-1]][a[i]]+(1-k[i-1])*d[a[i-1]][a[i]]); if(j)f[i][j][1]=min(f[i-1][j-1][0]+k[i]*d[a[i-1]][b[i]]+(1-k[i])*(d[a[i-1]][a[i]]),f[i-1][j-1][1]+k[i-1]*k[i]*d[b[i-1]][b[i]]+(1-k[i-1])*k[i]*d[a[i-1]][b[i]]+k[i-1]*(1-k[i])*d[b[i-1]][a[i]]+(1-k[i-1])*(1-k[i])*d[a[i-1]][a[i]]); } } for(int j=0;j<=m;++j){ ans=min(ans,f[n][j][0]); ans=min(ans,f[n][j][1]); } printf("%.2f\n",ans); return 0; }