Networking POJ - 1287 最小生成树板子题

#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5;
struct edge{
    int a,b,w;
}e[N];
bool cmp(edge a,edge b)
{
    return a.w<b.w;
}
int p[N];
int find(int x)
{
    if(p[x]!=x)
        p[x]=find(p[x]);
    return p[x];
}
int main()
{
    int n,m;
    while(cin>>n>>m&&n)
    {
        for(int i=1;i<=n;i++)
            p[i]=i;
        for(int i=0;i<m;i++)
        {
            int a,b,w;
            cin>>a>>b>>w;
            e[i]={a,b,w};
        }
        sort(e,e+m,cmp);
        int sum=0;
        for(int i=0;i<m;i++)
        {
            int a=find(e[i].a);
            int b=find(e[i].b);
            int w=e[i].w;
            if(a!=b)
            {
                p[a]=b;
                sum+=w;
            }
        }
        cout<<sum<<endl;
    }
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12239850.html

时间: 2024-11-05 18:47:59

Networking POJ - 1287 最小生成树板子题的相关文章

Constructing Roads POJ - 2421 最小生成树板子题

#include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N=110; int p[N]; struct edge{ int a; int b; int w; }e[N*N]; int map[N][N],flag[N][N],num,n; bool cmp(edge a,edge b) { return a.w<b.w; } int find(i

(最小生成树) Networking -- POJ -- 1287

链接: http://poj.org/problem?id=1287 代码: #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 210; const int INF = 0xfffffff; int n; int J[N][N], dist

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 <

B - Networking - poj 1287

有一些地方需要铺盖电缆,这些地方两点间的路可能不止一条,需要求出来至少需要多少电缆才能让所有的点都连接起来,当然,间接连接也算. ///////////////////////////////////////////////////////////////////////// #include<iostream>#include<cstring>#include<cstdio>#include<queue>#include<vector>usin

POJ 1287 Networking (最小生成树)

Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1287 Appoint description:  System Crawler  (2015-06-02) Description You are assigned to design network connections between certain poin

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

[2016-04-14][POJ][1287][Networking]

时间:2016-04-14 14:48:44 星期四 题目编号:[2016-04-14][POJ][1287][Networking] 题目大意:求最小生成树 分析:直接prim算法,更新边的时候,重边取最小值 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 50 + 10; const int inf = 0x3f3f3f3f; i

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

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

POJ 3278 Catch That Cow(BFS,板子题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00