建图
链式前向星
struct node{
int next,to,w;
node(){}
node(int a,int b,int c):next(a),to(b),w(c){}
}G[mx];
int head[mx];
int tot;
void init()
{
tot=0;
memeset(head,-1,sizeof(head));
}
void add(int u,int v, int w)
{
G[tot]=node(head[u],v,w);
head[u]=tot++;
}
void Traverse()
{
for(int u=1;u<=n;u++)
{
for(int i=head[u];i!=-1;i=G[i].next)
{
int v = G[i].to;
int w = G[i].w;
}
}
}
-
h
e
a
d
[
u
]
head[u]
head[u]表示与
u
u
u相连的最后一条边的
G
G
G结构体的下标
-
G
[
t
o
t
]
.
n
e
x
t
G[tot].next
G[tot].next用来记录与u相连的上一条边的
G
G
G结构体的下标
vector建图
struct node
{
int to,w;
node(){}
node(int a,int b):to(a),w(b){}
};
vector<node>G[mx];
void add(int u, int v ,int w)
{
G[u].push_back(node(v,w));
}
void Traverse()
{
for(int u=1;u<=n;u++)
{
for(int i=0;i<G[u].size();i++)
{
int v = G[u][i].v;
int w = G[u][i].w;
}
}
}