floyd?。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxv 105 #define maxn 10050 #define inf 0x7f7f7f7f7f7f7f7fLL using namespace std; int n,m,a[maxn]; long long map[maxv][maxv],ans=0; int main() { scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) scanf("%d",&a[i]); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) scanf("%lld",&map[i][j]); for (int k=1;k<=n;k++) for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) map[i][j]=min(map[i][j],map[i][k]+map[k][j]); for (int i=2;i<=m;i++) ans+=map[a[i-1]][a[i]]; printf("%lld\n",ans); return 0; }