prim

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
int G[][];
int vis[],lowc[];
int prim(int G[][],int n){
int i,j,p,minc,res=;
memset(vis,,sizeof(vis));//全部初值为0表示没有访问过;
vis[]=;
for(i=;i<=n;i++)
lowc[i]=G[][i];
for(i=;i<=n;i++){
minc=inf;
p=-;
for(j=;j<=n;j++){
if(vis[j]==&&lowc[j]<minc)
{minc=lowc[j];p=j;}
}
if(inf==minc) return -;//原图不连通
res+=minc;
vis[p]=;
for(j=;j<=n;j++){//更新lowc[]
if(vis[j]==&&lowc[j]>G[p][j])
lowc[j]=G[p][j];
}
}
return res;
}
int main(){
int n,m;
int x,y,w;
while(~scanf("%d %d",&n,&m)){
memset(G,inf,sizeof(G));
while(m--){
scanf("%d%d%d",&x,&y,&w);
G[x][y]=G[y][x]=w;
}
printf("%d\n",prim(G,n));
}
}

Kruskal

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 50005
int father[MAX], son[MAX];
int v, l; typedef struct Kruskal //存储边的信息
{
int a;
int b;
int value;
}; bool cmp(const Kruskal & a, const Kruskal & b)
{
return a.value < b.value;
} int unionsearch(int x) //查找根结点+路径压缩
{
return x == father[x] ? x : unionsearch(father[x]);
} bool join(int x, int y) //合并
{
int root1, root2;
root1 = unionsearch(x);
root2 = unionsearch(y);
if(root1 == root2) //为环
return false;
else if(son[root1] >= son[root2])
{
father[root2] = root1;
son[root1] += son[root2];
}
else
{
father[root1] = root2;
son[root2] += son[root1];
}
return true;
} int main()
{
int ncase, ltotal, sum, flag;
Kruskal edge[MAX];
scanf("%d%d", &v, &l);
ltotal = , sum = , flag = ;
for(int i = ; i <= v; ++i) //初始化
{
father[i] = i;
son[i] = ;
}
for(int i = ; i <= l ; ++i)
{
scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].value);
}
sort(edge + , edge + + l, cmp); //按权值由小到大排序
for(int i = ; i <= l; ++i)
{
if(join(edge[i].a, edge[i].b))
{
ltotal++; //边数加1
sum += edge[i].value; //记录权值之和
//cout<<edge[i].a<<"->"<<edge[i].b<<endl;
}
if(ltotal == v - ) //最小生成树条件:边数=顶点数-1
{
flag = ;
break;
}
}
if(flag) printf("%d\n", sum);
else printf("data error.\n");
return ;
}

51Nod 1212无向图最小生成树的更多相关文章

  1. 51Nod 1212 无向图最小生成树 (路径压缩)

    N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Input 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 &l ...

  2. 51nod 1212 无向图最小生成树(Kruskal模版题)

    N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Input 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 &l ...

  3. (图论)51NOD 1212 无向图最小生成树

    N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 <= M ...

  4. 51 nod 1212 无向图最小生成树(Kruckal算法/Prime算法图解)

    1212 无向图最小生成树 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 收起 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N < ...

  5. 51 nod 1212 无向图最小生成树

    http://www.51nod.com/Challenge/Problem.html#problemId=1212 代码 #include<bits/stdc++.h> using na ...

  6. 51Nod-1212 无向图最小生成树

    51Nod: 1212 无向图最小生成树. link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1212 1212  ...

  7. 51nod1212无向图最小生成树

    1212 无向图最小生成树 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树.   Inpu ...

  8. 加权无向图 最小生成树 Prim算法 延迟版和即时版 村里修路该先修哪

    本次要解决的问题是:你们村里那些坑坑洼洼的路,到底哪些路才是主干道? 小明:肯定是哪里都能到得了,并且去哪里都相对比较近,并且被大家共用程度高的路是啊! 具体是哪几条路呢?今天就可以给出准确答案 最小 ...

  9. 无向图最小生成树(prim算法)

    普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之和亦为最小.该算法于1930年由捷 ...

随机推荐

  1. Migrating from MapReduce 1 (MRv1) to MapReduce 2 (MRv2, YARN)...

    This is a guide to migrating from Apache MapReduce 1 (MRv1) to the Next Generation MapReduce (MRv2 o ...

  2. Bootstrap4用法

    #Bootstrap4 ## 网格系统- .col- 针对所有设备- .col-sm- 平板 - 屏幕宽度等于或大于 576px- .col-md- 桌面显示器 - 屏幕宽度等于或大于 768px)- ...

  3. hibernate 各历史版本下载 spring各历史版本下载

    hibernate 各历史版本下载http://sourceforge.net/projects/hibernate/files/ spring各历史版本下载http://www.springsour ...

  4. 思杰VDI提示“The VDI is not available”

    前言:困扰已久的问题终于解决. 问题:客户反馈无法连接VDI. 解决过程:1.登录后台查看VDI状态为关机状态尝试重新启动提示如下图: 2.判断此VDI的启动盘出现问题(注:本人环境无数据盘) 3.查 ...

  5. 源码-集合:ArrayList

    只是文章摘录,还未研究 JAVA ArrayList详细介绍(示例) http://www.jb51.net/article/42764.htm Jdk1.6 JUC源码解析汇总 - 永远保持敬畏之心 ...

  6. js面向对象过程

    var a = new  b(); 等价于 var a={}; a=b.prototype; b.call(a);

  7. Autofac小例子

    AutoFac是.net平台下的IOC容器产品.今天学习一下它的使用方法. 1.最简单的使用. public interface ITestDao { string SayHello(); } pub ...

  8. 「日常训练」 Soldier and Number Game (CFR304D2D)

    题意 (Codeforces 546D) 给定一个数x=a!b!" role="presentation">x=a!b!x=a!b!的形式,问其中有几个质因数. 分 ...

  9. 使用hibernate连接Oracle时的权限问题

    在使用hibernate对象关系映射连接和创建表的时候,会涉及到很多权限问题,有些数据库管理会将权限设的很细,我们可以根据后台日志错误和异常信息作出判断. 比如下图所示这个错误(这是我在给银行投产系统 ...

  10. (原) MatEditor部- UmateriaEditor中Texture使用过程(1)

    @author: 白袍小道 转载说明原处 插件同步在GITHUB: DaoZhang_XDZ     最后YY需求(手滑)(开黑前弄下,充数,见谅) 1.在理清楚基础套路和细节后,自定义纹理资源,并加 ...