三种存图方式
1.邻接矩阵
2.邻接表
邻接表是一种对于每个顶点,用链表来存储以该点为起点的边的数据结构。
# include <bits/stdc++.h>
using namespace std;
const int MAXN=250;
struct Edge{
int to,w,next;
}edge[MAXN<<1];
int head[MAXN];
int tot;
void add(int u,int v,int w)
{
edge[++tot].w=w;
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot;
return ;
}
void init()
{
tot=1;
memset(head,-1,sizeof(head));
memset(edge,0,sizeof(edge));
}
i i^1
# include <bits/stdc++.h>
using namespace std;
const int MAXN=1e4+100;
struct edge{
int to,w;
edge(int too,int ww){ to=too; w=ww; }
};
vector<edge> G[MAXN];
void addedge(int u,int v,int w)
{
G[u].push_back(edge(v,w));
}
int main()
{
3.链式前向星
# include <bits/stdc++.h>
using namespace std;
const int MAXN=1e4+100;
struct Edge{
int next,to,w;
}edge[MAXN];
int tot=0;