AcWing 847. 图中点的层次-java
原题链接
简单
作者:
Susu
,
2020-01-29 20:56:05
,
所有人可见
,
阅读 978
import java.util.*;
public class Main{
static int N = 100010;
static int[] q = new int[N];
static int[] d = new int[N];
static int[] e = new int[N];
static int[] ne = new int[N];
static int[] h = new int[N];
static int idx;
static void add(int a,int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for(int i=0;i<N;i++){
h[i]=-1;
}
while(m-->0){
int a = sc.nextInt();
int b = sc.nextInt();
add(a,b);
}
for(int i=0;i<N;i++){
d[i]=-1;
}
int hh=0,tt=0;
q[0] = 1;d[1]=0;
while(hh<=tt){
int t = q[hh++];
for(int i=h[t];i!=-1;i=ne[i]){
int j = e[i];
if(d[j]==-1){
d[j] = d[t]+1;
q[++tt] = j;
}
}
}
System.out.print(d[n]);
}
}
这是完全照着y总的写的把
我学的java,可以交流,留个qq
QQ1015458677,微信q13506235021
直接用队列你写过吗?