1. 程式人生 > >noip2016 換教室 概率+dp

noip2016 換教室 概率+dp

好的 noip main dig cin pre con 概率 ans

非常好的dp,繼續加油練習dp啊

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i=x;i<=y;i++)

using namespace std;

const int N=2e3+50;
const double inf=1e8+5;
const int V=305;
int n,m,v,e,c[N],d[N],g[V][V];
double k[N],f[N][N][2],ans;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while
(!isdigit(ch)){if(ch==-)f=-1;ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return x*f;} int main(){ n=read(),m=read(),v=read(),e=read(); rep(i,1,n) c[i]=read(); rep(i,1,n) d[i]=read(); rep(i,1,n) cin>>k[i]; memset(g,63,sizeof
g); rep(i,1,e){ int u=read(),v=read(),w=read(); g[u][v]=g[v][u]=min(g[u][v],w);} rep(k,1,v)rep(i,1,v)rep(j,1,v) g[i][j]=min(g[i][k]+g[k][j],g[i][j]); rep(i,1,v) g[i][i]=g[i][0]=g[0][i]=0; rep(i,0,n)rep(j,0,m) f[i][j][0]=f[i][j][1]=inf; f[1][0][0]=f[1][1][1]=0; rep(i,
2,n){ f[i][0][0]=f[i-1][0][0]+g[c[i-1]][c[i]]; rep(j,1,min(i,m)){ int k1=c[i-1],k2=d[i-1],k3=c[i],k4=d[i]; f[i][j][0]=min(f[i][j][0],f[i-1][j][0]+g[k1][k3]); f[i][j][0]=min(f[i][j][0],f[i-1][j][1]+g[k1][k3]*(1-k[i-1])+g[k2][k3]*k[i-1]); f[i][j][1]=min(f[i][j][1],f[i-1][j-1][0]+g[k1][k3]*(1-k[i])+g[k1][k4]*k[i]); f[i][j][1]=min(f[i][j][1],f[i-1][j-1][1]+g[k1][k3]*(1-k[i-1])*(1-k[i])+g[k2][k3]*k[i-1]*(1-k[i])+g[k1][k4]*(1-k[i-1])*k[i]+g[k2][k4]*k[i-1]*k[i]); } }ans=inf; rep(i,0,m)ans=min(ans,min(f[n][i][0],f[n][i][1])); printf("%.2lf\n",ans);return 0; }

懷挺,下一個

noip2016 換教室 概率+dp