直接上代码吧 Debug一下午 心累。。今天让我对memset有了完整的认识
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; template<class T> inline void read(T &_a){ bool f=0;int _ch=getchar();_a=0; while(_ch<'0' || _ch>'9'){if(_ch=='-')f=1;_ch=getchar();} while(_ch>='0' && _ch<='9'){_a=(_a<<1)+(_a<<3)+_ch-'0';_ch=getchar();} if(f)_a=-_a; } const int maxn=2001,maxm=2001; int n,m,v,e,dis[301][301],c[maxn],d[maxn]; double k[maxn],dp[maxn][maxn][2]; int main() { read(n); read(m); read(v); read(e); for (register int i=1;i<=n;++i) read(c[i]); for (register int i=1;i<=n;++i) read(d[i]); for (register int i=1;i<=n;++i) scanf("%lf",&k[i]); memset(dis,0x3f,sizeof(dis)); // 如果是0x7f,那么在之后的状态转移中会算爆int,直接GG for (register int i=1,a,b,w;i<=e;++i) { read(a); read(b); read(w); dis[b][a]=dis[a][b]=min(dis[a][b],w); } for (register int i=0;i<=v;++i) dis[i][i]=dis[0][i]=0; for (register int i=1;i<=v;++i) for (register int j=1;j<=v;++j) for (register int k=1;k<=v;++k) if(i!=j&&j!=k&&k!=i) dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]); memset(dp,0x7f,sizeof(dp)); dp[1][0][0]=dp[1][1][1]=0; for (register int i=2;i<=n;++i) for (register int j=0;j<=m&&j<=i;++j) { dp[i][j][0]=min(dp[i][j][0],dp[i-1][j][0]+dis[c[i-1]][c[i]]); dp[i][j][0]=min(dp[i][j][0],dp[i-1][j][1]+k[i-1]*dis[d[i-1]][c[i]]+(1.0-k[i-1])*dis[c[i-1]][c[i]]); if(j==0) continue; dp[i][j][1]=min(dp[i][j][1],dp[i-1][j-1][0]+k[i]*dis[c[i-1]][d[i]]+(1.0-k[i])*dis[c[i-1]][c[i]]); dp[i][j][1]=min(dp[i][j][1],dp[i-1][j-1][1]+k[i-1]*k[i]*dis[d[i-1]][d[i]]+k[i-1]*(1.0-k[i])*dis[d[i-1]][c[i]]+(1.0-k[i-1])*k[i]*dis[c[i-1]][d[i]]+(1.0-k[i-1])*(1.0-k[i])*dis[c[i-1]][c[i]]); } double ans=0x7fffffff; for (register int i=0;i<=m;++i) ans=min(ans,min(dp[n][i][0],dp[n][i][1])); printf("%.2lf",ans); return 0; }