比赛 |
2025暑期集训第5场图论专场 |
评测结果 |
AAAAAWAWWWWWW |
题目名称 |
Telephone |
最终得分 |
46 |
用户昵称 |
淮淮清子 |
运行时间 |
9.006 s |
代码语言 |
C++ |
内存使用 |
6.11 MiB |
提交时间 |
2025-07-09 11:50:11 |
显示代码纯文本
#include<iostream>
#include<vector>
#include<queue>
#include<ctime>
#include<cstring>
using namespace std;
const int MAXN = 5e4 + 5;
const int MAXK = 55;
const int IINF = 2147483646;
int n, k;
string s;
int b[MAXN];
bool r[MAXK][MAXK];
vector<pair<int, int> > e[MAXN];
int d[MAXN];
struct node{
int p, d;
bool operator<(const node& other)const{
return d > other.d;
}
};
int dij(){
priority_queue<node> q;
for(int i = 1;i <= n;i ++){
d[i] = IINF;
}
d[1] = 0;
q.push({1, 0});
while(!q.empty()){
node now = q.top();
if((double)clock() / CLOCKS_PER_SEC >= 0.98){
return now.d;
}
q.pop();
int u = now.p;
if(u == n) return now.d;
if(now.d > d[u]) continue;
for(auto x : e[u]){
int v = x.first, w = x.second;
if(d[v] > d[u] + w){
d[v] = d[u] + w;
q.push({v, d[v]});
}
}
}
return -1;
}
int main(){
freopen("telephone.in","r",stdin);
freopen("telephone.out","w",stdout);
cin.tie(0) -> ios::sync_with_stdio(0);
cin >> n >> k;
for(int i = 1;i <= n;i ++) cin >> b[i];
for(int i = 1;i <= k;i ++){
cin >> s;
for(int j = 1;j <= k;j ++){
r[i][j] = (s[j - 1] == '1');
}
}
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= n;j ++){
if((double)clock() / CLOCKS_PER_SEC >= 0.98){
cout << "-1\n";
return 0;
}
if(i != j && r[b[i]][b[j]]){
e[i].push_back({j, abs(i - j)});
}
}
}
cout << dij() << '\n';
return 0;
}