Linked List Implementation:
Struct Node
{
Int V; // The ending vertex of an edge.
Int W; // edge Length
Node * Next; // pointer to the next edge at the same starting point
} * First [N]; // first [u] points to the first edge starting from U.
Void Init ()
{
Memset (first, null, sizeof (first ));
}
Void add (int u, int V, int W) // Add an edge
{
Node * P = new node;
P-> V = V;
P-> W = W;
P-> next = Fisrt;
First [u] = P;
}
// When used, find the adjacent contact of u
For (node * P = first [u]; P! = NULL; P = p-> next)
{
// Handle the problem here
}
Array simulation:
struct node
{
int u, v, w;
int next;
}graph[1000];
int head[1000], t;
void init()
{
t = 1;
memset(head, 0, sizeof(memset));
}
void add(int u, int v, int w)
{
graph[t].u = u;
graph[t].v = v;
graph[t].w = w;
graph[t].next = head[u];
head[u] = t;
t++;
}
for(i = head[u]; i; i = graph[i].next)
{
...
}
Array Implementation of an Application: http://poj.org/problem? Id = 3321
Problem report: http://acm.sdut.edu.cn/bbs/read.php? Tid = 3170 & page = 1 #4303.