Kruskal 模板(Hdu - 1162)

最小生成树指的是在图上面找到权值最小的一棵树,并且保证图上所有的点都在这棵树上。

解决办法:Kruskal 算法(贪心思想)

/**
 *  Fuck you.
 *  I love you too.
 */

#include<bits/stdc++.h>
#define lson i<<2
#define rson i<<2|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define mem(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&-x)
#define enld endl
#define mian main
#define itn int
#define prinft printf

const double PI = acos (-1.0);
const int INF = 0x3f3f3f3f;
const int EXP = 1e-8;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;

using namespace std;

int n;
int ans;
int par[N];

struct ed {
    int s, e, cost;
} edge[MAXN];

bool cmp (ed a, ed b) {
    return a.cost < b.cost;
}

int Find (int a) {
    return a == par[a] ? a : par[a] = Find (par[a]);
}

void join (int a, int b) {
    par[Find (a)] = Find (b);
}

int main() {
    while (~scanf ("%d", &n)) {
        ans = 0;
        for (int i = 0; i < n; ++i) {
            scanf ("%d%d%d", &edge[i].s, &edge[i].e, &edge[i].cost);
            par[edge[i].s] = edge[i].s;
            par[edge[i].e] = edge[i].e;
        }
        sort (edge, edge + n, cmp);
        for (int i = 0; i < n; ++i) {
            if (Find (edge[i].s) != Find (edge[i].e)) {
                cout << edge[i].s << ‘ ‘ << edge[i].e << endl;
                join (edge[i].s, edge[i].e);
                ans += edge[i].cost;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/chunibyo/p/9409281.html

时间: 2024-12-19 20:02:18

Kruskal 模板(Hdu - 1162)的相关文章

hdu 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7428    Accepted Submission(s): 3770 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

HDU 1162 Eddy&#39;s picture【最小生成树,Prime算法+Kruskal算法】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9334    Accepted Submission(s): 4711 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

hdu 1162 Eddy&amp;#39;s picture (Kruskal算法,prim算法,最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法.保证联通的线路最短,典型的最小生成树问题. 方法一 . 通过不断找到最小的边来找到终于结果. Kruskal 算法 #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespac

hdu 1162 Eddy&#39;s picture (Kruskal算法,prim算法,最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法,保证联通的线路最短,典型的最小生成树问题. 方法一 , 通过不断找到最小的边来找到最终结果. Kruskal 算法 #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespac

HDU 1162 Eddy&#39;s picture(图论-最小生成树)

题目如下: Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7267    Accepted Submission(s): 3676 Problem Description Eddy begins to like painting pictures recently ,he is sure of himse

fft模板 HDU 1402

1 // fft模板 HDU 1402 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <math.h> 9 #include <memory.h> 10 #include <bits/stdc++.h> 11 using

hdu 1162 Eddy&#39;s picture(最小生成树算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6866    Accepted Submission(s): 3469 Problem Description Eddy begins to like p

hdu 1162 Eddy&#39;s picture

最小生成树裸题,没有初始化都能AC,看来测试数据只有一组 #include<iostream> #include<vector> #include<cmath> #include<cstdio> #define inf 1<<30 #define maxn 105 using namespace std; struct stu { double x,y; }; stu mapp[maxn]; int n; vector<int>roo

hdu 1162 Eddy&#39;s picture(最小生成树)

Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result i

(最小生成树)Eddy&#39;s picture -- hdu -- 1162

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8303    Accepted Submission(s): 4214 Problem Description Eddy begins to like painting pictures