1.用数组
head[MAXN],next[MAXM];// head[v]记录表头节点v的第一条边的标号,next[u]标号为u的边的下一条边。
struct node{
int s,e,w;
node(int s,int e,int w){
this->s=s; this->e=e; this->w=w;
}
node(){}
}t[MAXM];
void addnode(int s,int e,int w){//加边,头插法
t[e]=node(s,e,w);
next[e]=head[s];// 原头的第一条边改为新边的下一条边
head[s]=e++;// 将新边设为表头的第一条边
}
2.用vector容器
struct node{
int s,e,w;
node(int s,int e,int w){
this->s=s; this->e=e; this->w=w;
}
node(){}
}t[MAXM];
vector<node>vec[MAXN];
void addnode(int a,int b,int w){// 加边
vec[a].push_back(node(a,b,w));
}