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);