比赛 |
2025暑期集训第5场图论专场 |
评测结果 |
AAAEAETETTETT |
题目名称 |
Telephone |
最终得分 |
31 |
用户昵称 |
李奇文 |
运行时间 |
10.687 s |
代码语言 |
C++ |
内存使用 |
5.27 MiB |
提交时间 |
2025-07-09 11:27:09 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5,K=51;
int n,k;
int cc[K][K];
int b[N];
int hd[N*2],tot;
int d[N],vis[N];
bool inq[N];
struct node{
int f,v,w,nxt;
}e[2*N];
queue<int> q;
void add(int x,int y,int w){
tot++;
e[tot].v=y;
e[tot].f=x;
e[tot].w=w;
e[tot].nxt=hd[x];
hd[x]=tot;
}
bool re(int u,int v,int w){
if(d[v]>d[u]+w){
d[v]=d[u]+w;
return true;
}
return false;
}
void spfa(int fr){
q.push(fr);
inq[fr]=true;
d[fr]=0,vis[fr]=1;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]++;
inq[u]=false;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v,w=e[i].w;
if(re(u,v,w)&&!inq[v]){
q.push(v);
inq[v]=false;
}
}
}
}
int main(){
freopen("telephone.in","r",stdin);
freopen("telephone.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
memset(d,1000000007,sizeof(d));
for(int i=1;i<=k;i++){
for(int j=1;j<=k;j++){
char c;
cin>>c;
cc[i][j]=c-'0';
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(cc[b[i]][b[j]]==1&&i!=j){
add(i,j,abs(i-j));
}
}
}
spfa(1);
printf("%d",d[n]);
return 0;
}