zoukankan      html  css  js  c++  java
  • 【模板】最小生成树

    题目描述

    如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz

    输入输出格式

    输入格式:

    第一行包含两个整数N、M,表示该图共有N个结点和M条无向边。(N<=5000,M<=200000)

    接下来M行每行包含三个整数Xi、Yi、Zi,表示有一条长度为Zi的无向边连接结点Xi、Yi

    输出格式:

    输出包含一个数,即最小生成树的各边的长度之和;如果该图不连通则输出orz

    输入输出样例

    输入样例#1:
    4 5
    1 2 2
    1 3 2
    1 4 3
    2 3 4
    3 4 3
    输出样例#1:
    7

    说明

    时空限制:1000ms,128M

    数据规模:

    对于20%的数据:N<=5,M<=20

    对于40%的数据:N<=50,M<=2500

    对于70%的数据:N<=500,M<=10000

    对于100%的数据:N<=5000,M<=200000

    样例解释:

    所以最小生成树的总边权为2+2+3=7

    Kruskal代碼實現

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int n,m,a,b,ans;
     5 int fa[5010];
     6 struct nate{
     7     int q,z,bq;
     8 }edge[200010];
     9 int comp(const nate&x,const nate&y ){return x.bq<y.bq;}
    10 int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
    11 int main(){
    12     scanf("%d%d",&n,&m);
    13     for(int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].q,&edge[i].z,&edge[i].bq);
    14     sort(edge+1,edge+m+1,comp);
    15     for(int i=1;i<=n;i++) fa[i]=i;
    16     for(int i=1;i<=m;i++){
    17         a=find(edge[i].q);b=find(edge[i].z);
    18         if(a!=b){
    19             ans+=edge[i].bq;
    20             fa[b]=a;
    21         }
    22     }
    23     printf("%d
    ",ans);
    24     return 0;
    25 }
    PS:沒有Orz的數據。
    题目来源:洛谷
    题目描述 Description

    农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场。当然,他需要你的帮助。 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场。为了使花费最少,他想铺设最短的光纤去连接所有的农场。 你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并所用光纤最短的方案。 每两个农场间的距离不会超过100000

    输入描述 Input Description

    第一行: 农场的个数,N(3<=N<=100)。

    第二行..结尾: 接下来的行包含了一个N*N的矩阵,表示每个农场之间的距离。理论上,他们是N行,每行由N个用空格分隔的数组成,实际上,他们每行限制在80个字符以内,因此,某些行会紧接着另一些行。当然,对角线将会是0,因为线路从第i个农场到它本身的距离在本题中没有意义。

    输出描述 Output Description

    只有一个输出,是连接到每个农场的光纤的最小长度和。

    样例输入 Sample Input

    4

    0  4  9 21

    4  0  8 17

    9  8  0 16

    21 17 16  0

    样例输出 Sample Output

    28

    代码实现:

    Prim算法

     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 const int inf=100000000;
     5 int n,ans,p,b;
     6 int a[110],map[110][110];
     7 bool v[110];
     8 int main(){
     9     scanf("%d",&n); 
    10     for(int i=1;i<=n;i++)
    11     for(int j=1;j<=n;j++)
    12     scanf("%d",&map[i][j]);
    13     for(int i=1;i<=n;i++) a[i]=map[1][i];
    14     v[1]=1;
    15     for(int i=1;i<n;i++){
    16         p=inf;
    17         for(int i=1;i<=n;i++) if(!v[i]&&a[i]<p){p=a[i];b=i;}
    18         ans+=a[b];v[b]=1;
    19         for(int i=1;i<=n;i++) a[i]=min(a[i],map[b][i]);
    20     }
    21     printf("%d
    ",ans);
    22     return 0;
    23 }

    题目来源:CODE[VS]

  • 相关阅读:
    hdu 5726 GCD
    codeforces 982C Cut 'em all!
    codeforces 982B Bus of Characters
    codeforces 982A Row
    codeforces 983B XOR-pyramid
    codeforces 979D Kuro and GCD and XOR and SUM
    codeforces 983A Finite or not?
    codeforces 984B Minesweeper
    codeforces 979C Kuro and Walking Route
    codeforces 979B Treasure Hunt
  • 原文地址:https://www.cnblogs.com/J-william/p/6066941.html
Copyright © 2011-2022 走看看