一维方式存储邻接表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
typedef int mytype;
const int NV=105;
const int NE=10005*2;
int he[NV],ecnt;
struct edge
{
int v,next;
mytype l;
} E[NE];
//u是起点,v是终点,l是价值
void adde(int u,int v,mytype l)
{
E[++ecnt].v=v;
E[ecnt].l=l;
E[ecnt].next=he[u];
he[u]=ecnt;
}
///初始化:
ecnt=0;
memset(he,-1,sizeof(he));
///调用:
for (int i=he[u]; i!=-1; i=E[i].next) ;