邻接表大致是一个由各个点指向其所连接的点的表。但是我们可以不将点指向点,可以将边保存,用点指向边。
然后再用各个边的标号做一个数组,再用这个数组来记录路径。代码大概长这个样子。
(参考大佬的博客,地址如下https://www.cnblogs.com/ECJTUACM-873284962/p/6905416.html
(再附加两张图
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int u[100],v[100],w[100],first[100]={-1},next[100]={-1}; 7 int main() 8 { 9 for(int i=0;i<100;i++) 10 { 11 first[i]=-1; 12 next[i]=-1; 13 } 14 int vnum,snum,temp1,temp2,temp3; 15 cin>>vnum>>snum; 16 for(int i=0;i<snum;i++) 17 { 18 cin>>u[i]>>v[i]>>w[i]; 19 int x=first[u[i]]; 20 next[i]=x; 21 first[u[i]]=i; 22 } 23 /* for(int i=0;i<vnum;i++) 24 { 25 int x=first[i]; 26 cout<<i; 27 while(x!=-1) 28 { 29 cout<<' '<<v[x]; 30 x=next[x]; 31 } 32 cout<<endl; 33 }*/ 34 return 0; 35 }