1231 最優佈線問題

2022-05-03 17:15:09 字數 1378 閱讀 5268

時間限制: 1 s

空間限制: 128000 kb

題目等級 : ** silver

題解學校需要將n臺計算機連線起來,不同的2臺計算機之間的連線費用可能是不同的。為了節省費用,我們考慮採用間接資料傳輸結束,就是一台計算機可以間接地通過其他計算機實現和另外一台計算機連線。

為了使得任意兩台計算機之間都是連通的(不管是直接還是間接的),需要在若干臺計算機之間用網線直接連線,現在想使得總的連線費用最省,讓你程式設計計算這個最小的費用。

輸入描述 input description

輸入第一行為兩個整數n,m(2<=n<=100000,2<=m<=100000),表示計算機總數,和可以互相建立連線的連線個數。接下來m行,每行三個整數a,b,c 表示在機器a和機器b之間建立連線的話費是c。(題目保證一定存在可行的連通方案, 資料中可能存在權值不一樣的重邊,但是保證沒有自環)

輸出描述 output description

輸出只有一行乙個整數,表示最省的總連線費用。

樣例輸入 sample input

3 31 2 1

1 3 2

2 3 1

樣例輸出 sample output

資料範圍及提示 data size & hint

最終答案需要用long long型別來儲存

分類標籤 tags 點此展開 

最小生成樹

圖論並查集

樹結構**:

#include#include

#include

#include

#include

using

namespace

std;

int n,m,k,x,tx,ty,len;//

kruskal演算法

long

long tot=0

;const

int maxn=1000100

;int

fa[maxn],v[maxn],c[maxn];

struct

nodea[maxn];

int find(int

x)void unionn(int x,int

y)int cmp(const node &q,const node &h)

intmain()

for(int i=1;i<=n;i++) fa[i]=i;

sort(a+1,a+m+1

,cmp);

for(int i=1;i<=m;i++)

if(k==n-1) break

; }

printf(

"%lld\n

",tot);

return0;

}

1231 最優佈線問題 kruskal

題目描述 description 學校需要將n 臺計算機連線起來,不同的 2臺計算機之間的連線費用可能是不同的。為了節省費用,我們考慮採用間接資料傳輸結束,就是一台計算機可以間接地通過其他計算機實現和另外一台計算機連線。為了使得任意兩台計算機之間都是連通的 不管是直接還是間接的 需要在若干臺計算機之...

WikiOI 1231 最優佈線問題

這個問題讓無數人崩潰,而且沒有標程。我來貢獻乙個吧。從網上的標準kruskal 並查集 改的 我不會並查集 include include includeusing namespace std struct self self s 1000001 int a,b,m,n long long z 0 ...

CODEVS 1231最優佈線問題

include include include using namespace std int n,m,aa,bb,cc,cur 0,p 1000100 k 0 long long ans 0 struct nodea 1000100 int cmp struct node aa,struct no...