题解:
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int u,v,w,n,k,m,s,t,f[110][110],dis[10],vis[110][110]; bool flag[110][110][110]; inline int read(){ int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int main(int argc, char const *argv[]){ memset(f,0x3f,sizeof(f)); n=read(),k=read(),m=read(),s=read(),t=read(); for (int i = 1; i <= n; ++i){ dis[i]=read(); f[i][i]=0; } for (int i = 1; i <= k; ++i){ for (int j = 1; j <= k; ++j){ vis[i][j]=read(); } } for (int i = 1; i <= m; ++i){ u=read(),v=read(),w=read(); if (!vis[dis[v]][dis[u]]&&dis[u]!=dis[v]){ f[u][v]=min(w,f[u][v]); } if (!vis[dis[u]][dis[v]]&&dis[u]!=dis[v]){ f[v][u]=min(w,f[v][u]); } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ flag[i][j][dis[i]]=flag[i][j][dis[j]]=1; } } for(int k=1;k<=n;k++){ for(int i=1;i<=n;++i){ for(int j=1;j<=n;j++){ if(!vis[dis[k]][dis[i]]&&!vis[dis[j]][dis[k]]&&!flag[i][k][dis[j]]&&!flag[k][j][dis[i]]&&f[i][k]+f[k][j]<f[i][j]){ for(int t=1;t<=n;t++){ flag[i][j][t]=flag[i][k][t]||flag[k][j][t]; } flag[i][j][dis[k]]=1; f[i][j]=f[i][k]+f[k][j]; } } } } if(f[s][t]==0x3f3f3f3f) printf("-1 "); else printf("%d ",f[s][t]); return 0; }
一世安宁