链式前向星--邻接表存储图

转自http://blog.csdn.net/chl_3205/article/details/8159169

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define maxn 202
 4 struct ss
 5 {
 6     int v,w;
 7     int next;
 8 }edge[maxn];
 9 int top;
10 int head[maxn];
11 void add(int u,int v,int w)
12 {
13     edge[top].v=v;
14     edge[top].w=w;
15     edge[top].next=head[u];
16     head[u]=top++;
17 }
18 void print(int i)
19 {
20     for(int u=head[i];u!=0;u=edge[u].next)
21       printf("%d  %d  %d \n",i,edge[u].v,edge[u].w);
22 }
23 int main()
24 {
25     //freopen("Input.txt","r",stdin);
26     int n,m;
27     while(~scanf("%d%d",&n,&m))
28     {
29         memset(head,0,sizeof(head));
30         top=1;
31         int a,b,c;
32         while(m--)
33         {
34             scanf("%d%d%d",&a,&b,&c);
35             add(a,b,c);
36         }
37         scanf("%d",&m);
38         while(m--)
39         {
40             scanf("%d",&a);
41             print(a);
42         }
43     }
44     return 0;
45 }

 

你可能感兴趣的:(存储)