最小生成树模板

来源:互联网 发布:淘宝国际版apk 编辑:程序博客网 时间:2024/06/08 16:19

点击打开链接老哥就是稳,啥都知道;欢迎大家关注大佬的博客(点链接看大佬,现在不是将来也一定是哦)

时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 

收藏 
关注 
N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树。 
Input

第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量。(2 <= N <= 1000, 1 <= M <= 50000) 
第2 - M + 1行:每行3个数S E W,分别表示M条边的2个顶点及权值。(1 <= S, E <= N,1 <= W <= 10000)

Output

输出最小生成树的所有边的权值之和。

Input示例

9 14 
1 2 4 
2 3 8 
3 4 7 
4 5 9 
5 6 10 
6 7 2 
7 8 1 
8 9 7 
2 8 11 
3 9 2 
7 9 6 
3 6 4 
4 6 14 
1 8 8

Output示例

37

/*prim模板*/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define INF 0x3f3f3f#define N 1100int n, m;        //N为点的数量,M为边的数量bool vis[N];      //标记数组int dis[N];      //每点到现有集合的距离数组int G[N][N];     //建立邻接矩阵void init(){    for(int i = 1;i <= n; i++) {        for(int j = 1;j <= n; j++) {            if(i == j)                 G[i][j] = 0;            else                 G[i][j] = INF;        }    }   } int prim(int s){    int ans = 0;    memset(vis, false, sizeof(vis));    for(int i = 1;i <= n; i++) {        dis[i] = INF;    }    dis[s] = 0;    for(int i = 1;i <= n; i++) {        int min_1 = INF;        int next;        for(int j = 1;j <= n; j++) {            if(!vis[j] && dis[j]<min_1) {                min_1 = dis[j];                next = j;            }        }        if(min_1 == INF) {            break;        }         ans += min_1;        vis[next] = true;        for(int j = 1;j <= n; j++) {            if(!vis[j] && dis[j]>G[next][j]) {                dis[j] = G[next][j];            }        }    }return ans;}int main(){    scanf("%d%d",&n,&m);    init();    for(int i = 0;i < m; i++) {        int a, b, c;        scanf("%d%d%d",&a,&b,&c);        if(G[a][b] > c) {            G[a][b] = G[b][a] = c;        }           }        printf("%d\n",prim(1));return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
/*kruskal模板*/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int par[1100];void init(int n){    for(int i = 0;i <= n; i++) {        par[i] = i;    }}int find(int x) {    if(x == par[x]) {        return x;    }return par[x] = find(par[x]);}void unite(int x,int y){    x = find(x);    y = find(y);    if(x != y) {        par[x] = y;    }}struct node {    int a, b, c;}p[55000];bool cmp(node x, node y){    return x.c < y.c;}int main(){    int n, m;    scanf("%d%d",&n,&m);    init(n);    for(int i = 0;i < m; i++) {        scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c);    }    sort(p, p+m, cmp);    int sum = 0;    for(int i = 0;i < m; i++) {        if(find(p[i].a) != find(p[i].b)) {            unite(p[i].a,p[i].b);            sum += p[i].c;        }    }    printf("%d\n",sum);return 0;} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
原创粉丝点击