Networking——最小生成树模板题

题目链接

题意:

给你n个点 m条边  求最小生成树的权

题解:

最小生成树裸板子

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 50;
int f[maxn];
int n,cnt;
struct node
{
    int u,v,w;
    bool operator < (const node &a)const
    {
        return w<a.w;
    }
} edge[maxn*maxn];

int Find(int x)
{
    return x==f[x]?x:f[x]=Find(f[x]);
}
int kruskal()
{
    int ans=0;
    for(int i=0; i<=n; i++)f[i]=i;
    int sum=0;
    sort(edge,edge+cnt);
    for(int i=0; i<cnt; i++)
    {
        int x=edge[i].u;
        int y=edge[i].v;
        int fx=Find(x);
        int fy=Find(y);
        if(fx!=fy)
        {
            f[fx]=fy;
            ans+=edge[i].w;
            sum++;
        }
        if(sum==n-1)break;
    }
    return ans;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m) && n)
    {
        cnt=m;
        for(int i=0;i<=n;i++)f[i]=i;
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            edge[i].u=u;
            edge[i].v=v;
            edge[i].w=w;
        }
        printf("%d\n",kruskal());

    }
    return 0;
}

kruskal

原文地址:https://www.cnblogs.com/j666/p/11616873.html

时间: 2024-08-26 10:40:39

Networking——最小生成树模板题的相关文章

POJ 1287 Networking (最小生成树模板题)

Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between

还是畅通工程——最小生成树模板题

题目链接 题意: 给定n个村庄,m=(n*(n-1)/2)条关系  u,v,w 表示  u到 v之间的距离是 w 题解: 裸最小生成树模板题 代码: #include<iostream> #include<stdio.h> #include<math.h> #include<algorithm> #include<vector> using namespace std; typedef long long ll; const int maxn =

POJ2560 (最小生成树模板题)

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. Consider Dick's back to

(heu step 6.1.1)Constructing Roads(最小生成树模板题:求让n个点连通的最小费用)

题目: Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 207 Accepted Submission(s): 135   Problem Description There are N villages, which are numbered from 1 to N, and you should bu

HDU 1233 prim kruskal最小生成树模板题

A - 还是畅通工程 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小.请计算最小的公路总长度. Input 测试输入包含若干测试

最小生成树模板题-----P3366 【模板】最小生成树

题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出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 说明/提示 时空

最小生成树模板题POJ - 1287-prim+kruskal

POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出发,每次走这个点没走过的最小边权值,这样不断找下去就可以找出,本质就是贪心算法 而kruskal是利用并查集,先按照边权值大小排序,然后从小的边开始往里面添加边,利用并查集判断是否在一个联通分量里面(就是是否相连)如果不相 连就建立边,从而建图,注意,节点编号如果是从1->n,那么相应初始化就应该从

poj 1251 最小生成树模板题

Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200Sample Output 21630 prim算法 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <

POJ1258:Agri-Net(最小生成树模板题)

http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed conne