1.邻接矩阵 数组
g[N][N]二维数组存储,通常是稠密图 spa
注意数据范围,点的数目N不能太大code
2.邻接表blog
通常用于稀疏图class
static int N= , M=2*N; static int h[]=new int[N]; static int e[]=new int[N]; //M 无向边 ; N 有向边 static int ne[]=new int[N];//M static int w[]=new int[N]; static int idx; static void add(int a,int b,int c){ e[idx]=b; w[idx]=c; ne[idx]=h[a]; h[a]=idx++; } Arrays.fill(h,-1);