Kruskal Algorithm is based on Union-Find - quite intuitive.

#include <vector>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
using namespace std; struct Edge
{
Edge() :s(), t(), d() {}
Edge(unsigned rs, unsigned rt, unsigned rd) : s(rs), t(rt), d(rd) {} unsigned s;
unsigned t;
unsigned d; bool operator()(const Edge &e1, const Edge &e2)
{
if (e1.d != e2.d) return e1.d > e2.d;
return (e1.s + e1.d + e1.t) > (e2.s + e2.d + e2.t);
}
}; // Union-Set
unordered_map<unsigned, unsigned> pm; // smaller as parent
unsigned get_union_id(unsigned id)
{
if (pm.find(id) == pm.end())
{
pm[id] = id;
return id;
} unsigned ret = id;
while(ret != pm[ret]) ret = pm[ret];
return ret;
} void union_2id(unsigned id0, unsigned id1)
{
unsigned p0 = get_union_id(id0);
unsigned p1 = get_union_id(id1); if(p0 < p1) pm[p1] = id0;
else if(p0 > p1) pm[p0] = id1;
}
////////////////////////////////////// int main()
{
long n, m; cin >> n >> m; priority_queue<Edge, vector<Edge>, Edge> q, qs; // from -> to -> length
unordered_map<unsigned, unordered_map<unsigned, unsigned>> g;
for (int i = ; i < m; i++)
{
unsigned a, b, d; cin >> a >> b >> d;
q.push(Edge(min(a,b), max(a,b), d));
g[a][b] = g[b][a] = d;
} unsigned long long ret = ; // Step 1
unsigned s; cin >> s;
for (auto &kv : g[s])
{
unsigned a = s;
unsigned b = kv.first;
qs.push(Edge(min(a, b), max(a, b), kv.second));
}
// pick first
const Edge &first = qs.top();
ret += first.d;
union_2id(first.s, first.t); while (!q.empty())
{
Edge picked = q.top(); q.pop(); if (get_union_id(picked.s) == get_union_id(picked.t)) continue;
union_2id(picked.s, picked.t); ret += picked.d;
}
cout << ret << endl;
return ;
}

HackerRank "Kruskal (MST): Really Special Subtree"的更多相关文章

  1. HackerRank "Prim's (MST) : Special Subtree"

    An intuitive Prim algorithm impl. #include <vector> #include <iostream> #include <que ...

  2. The Unique MST(次小生成树)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22335   Accepted: 7922 Description Give ...

  3. Kruskal最小生成树

    并查集+kruskal==>MST 效率很低 #include <iostream> using namespace std; #define MAX 105 //自己设置最大值 / ...

  4. [NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)

    一开始觉得是网络流..仔细一看应该是最短路,再看数据范围..呵呵不会写...这道题是最大生成树+最近公共祖先.第一次写..表示各种乱.. 因为要求运输货物质量最大,所以路径一定是在最大生成树上的.然后 ...

  5. 最小生成树问题:Kruskal算法 AND Prim算法

    Kruskal算法: void Kruskal ( ) {     MST = { } ;                           //边的集合,最初为空集     while( Edge ...

  6. POJ——T1679 The Unique MST

    http://poj.org/problem?id=1679 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30120   ...

  7. Algorithms & Data structures in C++& GO ( Lock Free Queue)

    https://github.com/xtaci/algorithms //已实现 ( Implemented ): Array shuffle https://github.com/xtaci/al ...

  8. CCF-CSP题解 201812-4 数据中心

    题目要求最长边最小的生成树.好吧,这就是一道kruskal MST题. #include <bits/stdc++.h> const int maxn = 50000; const int ...

  9. NetworkX系列教程(10)-算法之二:最小/大生成树问题

    小书匠 Graph 图论  重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定 ...

随机推荐

  1. jq知识总结

    jQuery   jQuery基本选择器: id选择器     $("#div1") class选择器   $(".div1") 元素选择器   $(" ...

  2. 使用OpenFileDialog会更改默认程序目录

    这个问题可能只有在特定的程序中会发现:当我们在程序中使用相对路径时是依赖于当前目录的.所以在使用类似代码: XElement rootNode = XElement.Load(@"zips/ ...

  3. 常用的获取时间差的sql语句

    "select count(*) from [注册] where datediff(day,time,getdate())<1";//获取当天注册人员数 sql=" ...

  4. eclipse配置ros cakin编译环境

    先安装eclipse,之前的博客:http://www.cnblogs.com/CZM-/p/5942435.html Catkin-y 方法使用catkin无法make eclipse工程,生成pr ...

  5. jsoup 简介

    Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从HTML中攫取你所需的信 ...

  6. 黑马程序员——OC语言 类和对象

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)类 1)类的声明 代码编写 ①定义一个Car类,拥有2个属性:轮子数 ...

  7. Android M新特性之Permissions

    User does not have to grant any permissions when they install or upgrade the app. Instead, the app r ...

  8. JAVA ,Map接口 ,迭代器Iterator

    1.    Map 接口概述 java.util.Map 接口描述了映射结构, Map 接口允许以键集.值集合或键 - 值映射关系集的形式查看某个映射的内容. Java 自带了各种 Map 类. 这些 ...

  9. poj2387 spfa求最短路

    //Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...

  10. php大力力 [045节] 兄弟连高洛峰 PHP教程 2014年[已发布,点击下载]

    http://www.verycd.com/topics/2843130/ 第1部分 WEB开发入门篇第1章LAMP网站构建1.[2014]兄弟连高洛峰 PHP教程1.1.1 新版视频形式介绍[已发布 ...