题解 P7646 【[COCI2012-2013#5] HIPERCIJEVI】
Unordered_OIer · · 题解
P7646 题解
Description
一根超级管道能够将
Solution
最短路裸题。
考虑将一根所谓的
看到目前为止其他两篇题解讲的答案需要 dis[n]/2+1 都不是很清楚,这里再讲一遍:
先看一下一个“超级管道”连接
但是,照着我们的思路,我们会把它变成:
原本
因为要求的是点数,所以答案是
最后的话这题卡空间不能开 long long,开了 long long 你就会和我一样提交了若干遍最后两个点
Code
inline void bfs(){
memset(dst,-1,sizeof dst);
queue<int> q;dst[1]=1;
q.push(1);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=hd[u];i;i=es[i].nxt){
int v=es[i].to;
if(dst[v]==-1){
dst[v]=dst[u]+1;
q.push(v);
}
}
}
}
int main(){
n=read(),k=read(),m=read();
for(int i=1;i<=m;i++)
for(int j=1;j<=k;j++){
int x=read();
add(n+i,x),add(x,n+i);
}
bfs();
writeln(dst[n]==-1?-1:dst[n]/2+1);
return 0;
}