kruskal最小生成树

来源:互联网 发布:大鼠血数据 编辑:程序博客网 时间:2024/05/18 01:54
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">(1) </span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><a target=_blank href="http://www.wutianqi.com/?p=1286" style="color: rgb(51, 102, 153); text-decoration: none;">克鲁斯卡尔算法</a></span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">图的存贮结构采用边集数组,且权值相等的边在数组中排列次序可以是任意的.该方法对于边相对比较多的不是很实用,浪费时间.</span>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></span><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">方法:将图中边按其权值由小到大的次序顺序选取,若选边后不形成回路,则保留作为一条边,若形成回路则除去.依次选够(n-1)条边,即得最小生成树.(n为顶点数)<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/1.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1316" title="kruskal1" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/1.gif" width="113" height="113" style="border: none; max-width: 100%;" /></a>第一步:由边集数组选第一条边<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/2.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1317" title="kruskal2" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/2.gif" width="48" height="87" style="border: none; max-width: 100%;" /></a>第二步:选第二条边,即权值为2的边<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/3.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1318" title="3" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/3.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a>第三步:选第三条边,即权值为3的边<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/4.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1319" title="4" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/4.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a>第四步:选第四条边,即权值为4的边<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/5.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1320" title="5" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/5.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a>第五步:选第五条边<a target=_blank class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2010/09/6.gif" style="color: rgb(51, 102, 153); text-decoration: none;"><img class="aligncenter size-full wp-image-1321" title="6" alt="" src="http://www.wutianqi.com/wp-content/uploads/2010/09/6.gif" width="100" height="103" style="border: none; max-width: 100%;" /></a></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p>
int r[maxn],p[maxn],u[maxn],v[maxn],w[maxn];int n,m,ans=0;bool cmp(const int i, const int j){return w[i]<w[j];}int find(int x){return p[x]==x?x:find(p[x]);}//并查集int krus(){    for(int i=1;i<=n;i++)p[i]=i;//初始化并查集    for(int i=1;i<=m;i++)r[i]=i;//初始化边    sort(r+1,r+m+1,cmp);    for(int i=1;i<=m;i++){    int e=r[i]; int x=find(u[e]);int y=find(v[e]);    if(x!=y){            printf("%d-----%d\n",x,y);//打印连线的两个点            ans+=w[e];            p[x]=y;//加入        }    }        return ans;}

0 0
原创粉丝点击