非常好的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+;
const double inf=1e8+;
const int V=;
int n,m,v,e,c[N],d[N],g[V][V];
double k[N],f[N][N][],ans; inline int read(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+(ch^);ch=getchar();}
return x*f;} int main(){
n=read(),m=read(),v=read(),e=read();
rep(i,,n) c[i]=read();
rep(i,,n) d[i]=read();
rep(i,,n) cin>>k[i];
memset(g,,sizeof g);
rep(i,,e){
int u=read(),v=read(),w=read();
g[u][v]=g[v][u]=min(g[u][v],w);}
rep(k,,v)rep(i,,v)rep(j,,v)
g[i][j]=min(g[i][k]+g[k][j],g[i][j]);
rep(i,,v) g[i][i]=g[i][]=g[][i]=;
rep(i,,n)rep(j,,m) f[i][j][]=f[i][j][]=inf;
f[][][]=f[][][]=;
rep(i,,n){
f[i][][]=f[i-][][]+g[c[i-]][c[i]];
rep(j,,min(i,m)){
int k1=c[i-],k2=d[i-],k3=c[i],k4=d[i];
f[i][j][]=min(f[i][j][],f[i-][j][]+g[k1][k3]);
f[i][j][]=min(f[i][j][],f[i-][j][]+g[k1][k3]*(-k[i-])+g[k2][k3]*k[i-]);
f[i][j][]=min(f[i][j][],f[i-][j-][]+g[k1][k3]*(-k[i])+g[k1][k4]*k[i]);
f[i][j][]=min(f[i][j][],f[i-][j-][]+g[k1][k3]*(-k[i-])*(-k[i])+g[k2][k3]*k[i-]*(-k[i])+g[k1][k4]*(-k[i-])*k[i]+g[k2][k4]*k[i-]*k[i]);
}
}ans=inf;
rep(i,,m)ans=min(ans,min(f[n][i][],f[n][i][]));
printf("%.2lf\n",ans);return ;
}
怀挺,下一个