这个存储想了好几天,也还是不太明白,写一下理一下思绪
邻接表存储有向或无向图
有u,v,w 三个数组分别存储起点,终点,权值
有一个first数组,和一个next数组,first数组,next数组都存储的边的编号
即u v w输入顺序,可以查某个点k开始的所有边
first[k]->next[first[k]]->next[next[first[k]]]->next[next[next[first[k]]]]->-1
package 啊哈;
import java.util.Arrays;
import java.util.Scanner;
public class 邻接表 {
static int[] u=new int[10];
static int[] v=new int[10];
static int[] w=new int[10];
static int[] first=new int[10];
static int[] next=new int[10];
static int n,m;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
Arrays.fill(u,0);
Arrays.fill(v,0);
Arrays.fill(w,0);
Arrays.fill(first,-1);
Arrays.fill(next,0);
for(int i=1;i<=m;i++){
u[i]=sc.nextInt();
v[i]=sc.nextInt();
w[i]=sc.nextInt();
next[i]=first[u[i]];//next数组存储上一条该点u[i]相连的边的编号,也是上上一个与该点u[i]相连的边在next数组中的存储位置
first[u[i]]=i;first数组存储边的编号,也是上一个与该点u[i]相连的边在next数组中的存储位置
}
for(int i=1;i<=n;i++){
int k=first[i];
while(k!=-1){
System.out.println(u[k]+" "+v[k]+" "+w[k]);
k=next[k];//下一条边在数组中存储的位置
}
}
}
}