比赛 |
2025暑期集训第5场图论专场 |
评测结果 |
AAAAAAATAAAAA |
题目名称 |
Telephone |
最终得分 |
92 |
用户昵称 |
pcx |
运行时间 |
2.019 s |
代码语言 |
C++ |
内存使用 |
4.67 MiB |
提交时间 |
2025-07-09 11:32:47 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,K=55,INF=0x3f3f3f3f;
typedef pair<int,int> PII;
int n,k;
int b[N],dis[N];
bool a[K][K];
vector<int> pos[K];
int dijkstra(){
memset(dis,0x3f,sizeof(dis));dis[1]=0;
priority_queue<PII,vector<PII>,greater<PII> > pq;
pq.push(make_pair(0,1));
while(!pq.empty()){
int d=pq.top().first,u=pq.top().second;
pq.pop();
if (u==n){
return d;
}
if (d>dis[u]) continue;
for(int i=1;i<=k;i++){
if(a[b[u]][i]){
for(int j=0;j<(int)pos[i].size();j++){
int x=pos[i][j];
if(dis[x]>dis[u]+abs(u-x)){
dis[x]=dis[u]+abs(u-x);
pq.push(make_pair(dis[x],x));
}
}
}
}
}
return dis[n];
}
int main() {
freopen("telephone.in", "r", stdin);
freopen("telephone.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>b[i];
pos[b[i]].push_back(i);
}
for (int i=1;i<=k;i++) {
string s;
cin>>s;
for (int j=1;j<=k;j++) {
if(s[j-1]=='0'){
a[i][j]=0;
}else{
a[i][j]=1;
}
}
}
int f=dijkstra();
if(f==0x3f3f3f3f){
cout<<-1<<endl;
}else{
cout<<f<<endl;
}
return 0;
}