https://www.luogu.com.cn/problem/P5767
本地评测和洛谷IDE均没问题,但交到洛谷上输出都是"NO"。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e5+10;
ll to[N],nxt[N],head[N],val[N],tot;
ll n,m,x,a[N],dist[N],cnt;
bool vis[N];
string s;
void add(ll x,ll y,ll v){
to[tot]=y;
nxt[tot]=head[x];
val[tot]=v;
head[x]=tot++;
}
void init(){
for(int i=0;i<N;i++){
head[i]=-1;
dist[i]=INT_MAX;
}
}
priority_queue<pair<ll,ll> > q;
void dijkstra(ll root){
dist[root]=0;
q.push({0,root});
while(!q.empty()){
ll x=q.top().second;
q.pop();
if(vis[x]) continue;
vis[x]=true;
for(int i=head[x];i!=-1;i=nxt[i]){
ll next=to[i];
if(dist[x]+val[i]<dist[next]){
dist[next]=dist[x]+val[i];
q.push({-dist[next],next});
}
}
}
}
ll st(string f){
ll x=0;
for(int i=0;i<(ll)f.length();i++) x=x*10+f[i]-'0';
return x;
}
void does(string s){
for(int i=0;i<(ll)s.length();i++){
if(s[i]==' '){
string f=s.substr(0,i);
a[++cnt]=st(f);
s.erase(0,i+1);
i=0;
}
}
a[++cnt]=st(s);
}
int main(){
init();
cin>>m>>n;
getchar();
while(m--){
getline(cin,s);
cnt=0;
does(s);
for(int i=1;i<=cnt;i++){
for(int j=i+1;j<=cnt;j++){
add(a[i],a[j],1);
}
}
}
dijkstra(1);
if(dist[n]!=INT_MAX) printf("%lld",dist[n]-1);
else printf("NO");
return 0;
}