import java.util.*;
class Connection{
String node1;
String node2;
int cost;
public Connection(String a, String b, int c){
node1 = a;
node2 = b;
cost = c;
}
}
public class Solution {
static class DisjointSet
{
Set<String> set;
Map<String, String> map;
int count;
public DisjointSet()
{
count = 0;
set = new HashSet<>();
map = new HashMap<>();
}
public void MakeSet(String s)
{
if(!set.contains(s))
{
count++;
set.add(s);
map.put(s, s);
}
}
public String Find(String s)
{
if(!set.contains(s)) return null;
if(s.equals(map.get(s))) return s;
String root = this.Find(map.get(s));
map.put(s, root);
return root;
}
public void Union(String s, String t)
{
if(!set.contains(s) || !set.contains(t)) return;
if(s.equals(t)) return;
count--;
map.put(s, t);
}
}
static class ConnectionComparator1 implements Comparator<Connection>
{
@Override
public int compare(Connection a, Connection b)
{
return a.cost - b.cost;
}
}
static class ConnectionComparator2 implements Comparator<Connection>
{
@Override
public int compare(Connection a, Connection b)
{
if(a.node1.equals(b.node1)) return a.node2.compareTo(b.node2);
else return a.node1.compareTo(b.node1);
}
}
public static List<Connection> getMST(List<Connection> connections)
{
Comparator<Connection> comparator1 = new ConnectionComparator1();
Comparator<Connection> comparator2 = new ConnectionComparator2();
Collections.sort(connections, comparator1);
DisjointSet set = new DisjointSet();
List<Connection> res = new ArrayList<>();
for(Connection itr: connections)
{
set.MakeSet(itr.node1);
set.MakeSet(itr.node2);
}
for(Connection itr: connections)
{
String s = set.Find(itr.node1);
String t = set.Find(itr.node2);
if(!s.equals(t))
{
set.Union(s, t);
res.add(itr);
if(set.count == 1) break;
}
}
if(set.count == 1)
{
Collections.sort(res, comparator2);
return res;
}
else return new ArrayList<Connection>();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Connection> connections = new ArrayList<>();
// connections.add(new Connection("Acity","Bcity",1));
// connections.add(new Connection("Acity","Ccity",2));
// connections.add(new Connection("Bcity","Ccity",3));
connections.add(new Connection("A","B",6));
connections.add(new Connection("B","C",4));
connections.add(new Connection("C","D",5));
connections.add(new Connection("D","E",8));
connections.add(new Connection("E","F",1));
connections.add(new Connection("B","F",10));
connections.add(new Connection("E","C",9));
connections.add(new Connection("F","C",7));
connections.add(new Connection("B","E",3));
connections.add(new Connection("A","F",1));
List<Connection> res = getMST(connections);
for (Connection c : res){
System.out.println(c.node1 + " -> " + c.node2 + " cost : " + c.cost);
}
}
}
Minimum Spanning Tree
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來笨觅,“玉大人拦耐,你說我怎么就攤上這事〖#” “怎么了杀糯?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長炮温。 經(jīng)常有香客問我火脉,道長,這世上最難降的妖魔是什么柒啤? 我笑而不...
- 正文 為了忘掉前任倦挂,我火速辦了婚禮,結(jié)果婚禮上担巩,老公的妹妹穿的比我還像新娘方援。我一直安慰自己,他們只是感情好涛癌,可當(dāng)我...
- 文/花漫 我一把揭開白布犯戏。 她就那樣靜靜地躺著,像睡著了一般拳话。 火紅的嫁衣襯著肌膚如雪先匪。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼艺糜,長吁一口氣:“原來是場噩夢啊……” “哼幢尚!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起送滞,我...
- 序言:老撾萬榮一對情侶失蹤侠草,失蹤者是張志新(化名)和其女友劉穎犁嗅,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體晤碘,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年园爷,在試婚紗的時候發(fā)現(xiàn)自己被綠了宠蚂。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站沼沈,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏列另。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一页衙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拷姿,春花似錦、人聲如沸旱函。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽含长。三九已至,卻和暖如春拘泞,著一層夾襖步出監(jiān)牢的瞬間枕扫,已是汗流浹背陪腌。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像砾赔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子暴心,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- Given a binary treestruct TreeLinkNode {TreeLinkNode *lef...
- 層次遍歷法 用層次遍歷方法很簡單酷勺,但是用深度遍歷法則需要好好思考思考,遞歸就是整體考慮脆诉,就把run當(dāng)做了求最小值的方法
- 原題是: 代碼是: 要注意的是: 1.python 里沒有&&, 而是and; 沒有Null击胜,而是None.2.調(diào)...
- Easy 給定二叉樹偶摔,返回最淺深度暇唾。 簡單的遞歸問題辰斋,與二叉樹最大深度類似策州。注意:如果一棵樹只有左或右節(jié)點(diǎn)宫仗,這個樹...
- Aug 3 2017 Update 遞歸方法1: 樹只有一個孩子的情況要去遞歸執(zhí)行它的孩子,depth要+1藕夫。最后...