Adjacent table template
LThe Processing Method of the adjacent table is as follows:LVertices in the graph are stored in a one-dimensional array. Of course, vertices can also be stored in a single-chain table. However, Arrays can easily read vertex information, making it easier.LIn the figure, all the adjacent points of each vertex Vi form a linear table. because the number of adjacent points is not sure, we choose to use a single-chain table for storage.
1 #include<iostream> 2 using namespace std; 3 struct node 4 { 5 int u; 6 int v; 7 int w; 8 int next; 9 }a[10001];10 int head[1001];11 int num=1;12 void f(int p,int b,int c)13 {14 a[num].u=p;15 a[num].v=b;16 a[num].w=c;17 a[num].next=head[p];18 head[p]=num++;19 }20 int main()21 {22 int n,m;23 cin>>n>>m;24 for(int i=1;i<=n;i++)25 head[i]=-1;26 for(int i=1;i<=m;i++)27 {28 int p,b,c;29 cin>>p>>b>>c;30 f(p,b,c);31 }32 33 cout<<"****************************"<<endl;34 for(int k=1;k<=n;k++)35 {36 int i=head[k]; 37 cout<<"k:";38 while(i!=-1)39 {40 cout<<"-->"<<a[i].v;41 i=a[i].next;42 }43 cout<<"-->"<<-1<<" ";44 cout<<endl;45 }46 47 return 0;48 }