码迷,mamicode.com
首页 > 编程语言 > 详细

图论加边算法

时间:2015-07-27 20:41:22      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

很多图论的算法都有一个函数

struct Edge {
    int to;
    int w;
    int next;
} edge[N * 2];

int cnt_edge = 0;

void add_edge(int from, int to, int w)
{
    edge[cnt_edge].to = to;
    edge[cnt_edge].w = w;
    edge[cnt_edge].next = head[from];
    head[from] = cnt_edge++;
}

memset(head, -1, sizeof head);

我纠结了好久这个函数的意思……

实际上是用邻接表存一个图。

cnt_edge是给每一个边标号,从0开始。

edge[i].to 表示第i条边指向哪个点,edge[i].next表示第i条边的下一条边的序号。

head[from]表示以第from为初始结点的边的序号。

例如图(随便画的= =#):

技术分享

存在边1,3   1,4   2,5   2,6   4,3   5,3  六条边

依次调用函数add_edge之后,可以得到

edge[0].to=3; edge[0].next=-1; head[1]=0;

edge[1].to=4; edge[1].next=0; head[1]=1;

edge[2].to=5; edge[2].next=-1; head[2]=2;

edge[3].to=6; edge[3].next=5; head[2]=3;

edge[4].to=3; edge[4].next=-1; head[4]=4;

edge[5].to=3; edge[5].next=-1; head[5]=5;

用图来表示就是:

技术分享

喵。箭头的方向该怎么画呢。

 

图论加边算法

标签:

原文地址:http://www.cnblogs.com/wenruo/p/4680930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!