推薦:(個人理解,若有錯誤,歡迎指正)
先百度百科,看懂後再看這個版本的。
const int maxn=1e5+1;
/*
前向星的優勢:
通常用在點的數目太多,或兩點之間有多條弧的時候。
一般在別的數據結構不能使用的時候才考慮用前向星。
除了不能直接用起點終點定位以外,前向星幾乎是完美的
*/
//前向星的構建
//模擬逆序建立鏈表的過程
///鏈式前向星
struct node
{
int next,to,w;
}Edge[maxn];
int head[maxn];
int cnt;
void Add(int u,int v,int w)
{
Edge[cnt].w=w;
Edge[cnt].to= v;
///等同語句
//p->next=head->next;
Edge[cnt].next=head[u];
//head->next=p;
head[u]=cnt++;
}
///vector數組版(來源百度百科)
//具體就不寫了,百度一下
至於遍歷:
for(int i=head[u];~i;i=edge[i].next)
推薦博客:
http://blog.csdn.net/acdreamers/article/details/16902023
Ps(自我感覺前向星和這篇博客 挺像的,所以就加上了,個人觀點(銘記))
http://blog.csdn.net/qq_33279781/article/details/52169408