#include "bits/stdc++.h" using namespace std; const int N=202; #define Min(x,y) x=min(x,y) int n,m,a[N][N],f[1002][N][N]; int p[1002]; void sov(){ int i,j,k; memset(f,0x3f,sizeof f); f[0][1][2]=0; p[0]=3; for(i=0;i<=m;i++) for(j=1;j<=n;j++) for(k=1;k<=n;k++){ if(j==p[i]||k==p[i]||j==k)continue; Min(f[i+1][j][k],f[i][j][k]+a[p[i]][p[i+1]]); Min(f[i+1][p[i]][k],f[i][j][k]+a[j][p[i+1]]); Min(f[i+1][j][p[i]],f[i][j][k]+a[k][p[i+1]]); } int ans=1e9; for(i=1;i<=n;i++) for(j=1;j<=n;j++){ if(i==j||i==p[m]||j==p[m])continue; ans=min(ans,f[m][i][j]); } cout<<ans<<endl; } main(){ cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>a[i][j]; for(int i=1;i<=m;i++) cin>>p[i]; sov(); }
标签:std,服务,int,274,sov,移动,1002 From: https://www.cnblogs.com/towboa/p/16947356.html