1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<bits/stdc++.h>
#define ull unsigned long long #define mmst0(x) memset(x,0,sizeof(x)) #define mmst3f(x) memset(x,0x3f,sizeof(x)) #define pb(x) push_back(x)
using namespace std;
const int INF=0x3f3f3f3f,MOD=1e9+7;
int read(){char c;int num,f=1;while(c=(char)getchar(),!isdigit(c))if(c=='-')f=-1;num=(int)(c-'0');while(c=(char)getchar(),isdigit(c))num=num*10+(int)(c-'0');return num*f;}
int l,n,ans=INF; int p[1003]={3}; int c[203][203]; int f[1003][203][203];
void work() { scanf("%d%d",&l,&n); for(int i=1;i<=l;i++) for(int j=1;j<=l;j++) scanf("%d",&c[i][j]); for(int i=1;i<=n;i++) scanf("%d",p+i); mmst3f(f); f[0][1][2]=0; for(int i=0;i<n;i++) { for(int x=1;x<=l;x++) { for(int y=1;y<=l;y++) { int z=p[i],nxt=p[i+1],now=f[i][x][y]; if(x==y || x==z || y==z) continue; f[i+1][x][y]=min(f[i+1][x][y],now+c[z][nxt]); f[i+1][z][y]=min(f[i+1][z][y],now+c[x][nxt]); f[i+1][x][z]=min(f[i+1][x][z],now+c[y][nxt]); } } } for(int x=1;x<=l;x++) for(int y=1;y<=l;y++) { if(f[n][x][y]<ans) ans=f[n][x][y]; } cout<<ans<<endl; return; }
signed main() { int T=1; for(int Case=1;Case<=T;Case++) { work(); } return 0; }
|