国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

hdu1233-還是暢通工程(Kruskal)

2019-11-11 02:41:02
字體:
來源:轉載
供稿:網友

還是暢通工程 某省調查鄉村交通狀況,得到的統計表中列出了任意兩村莊間的距離。省政府“暢通工程”的目標是使全省任何兩個村莊間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可),并要求鋪設的公路總長度為最小。請計算最小的公路總長度。 Input 測試輸入包含若干測試用例。每個測試用例的第1行給出村莊數目N ( < 100 );隨后的N(N-1)/2行對應村莊間的距離,每行給出一對正整數,分別是兩個村莊的編號,以及此兩村莊間的距離。為簡單起見,村莊從1到N編號。 當N為0時,輸入結束,該用例不被處理。 Output 對每個測試用例,在1行里輸出最小的公路總長度。 Sample Input 3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0 Sample Output 3 5

簡單模板

#include<iostream>#include<cstdio>#include<string.h>#include<algorithm>using namespace std;const int maxn=105;int fa[maxn];void init(){ for(int i=0;i< maxn;i++) fa[i]=i;}int Find(int x){ if(fa[x]== x) return fa[x]; else return fa[x]=Find(fa[x]);}void Union(int x,int y){ int fx=Find(x),fy=Find(y); if( fx!= fy) fa[fx] =fy;}typedef struct{ int st,ed,cost;}Edge;Edge edge[10005];int cmp(Edge a,Edge b){ return a.cost < b.cost;}int main(){ ios_base::sync_with_stdio(false); int n; while(cin>>n,n){ int t1,t2,t3; int m=n*(n-1)/2; for(int i=0;i<m;i++){ cin>>t1>>t2>>t3; edge[i].st= t1; edge[i].ed= t2; edge[i].cost =t3; } init(); sort(edge,edge+m,cmp); int rst=n; int tot_cost =0; for(int i=0; i<m && rst >1 ;i++){ if(Find(edge[i].st) != Find(edge[i].ed)){ Union(edge[i].st,edge[i].ed); rst--; tot_cost +=edge[i].cost; } } cout<<tot_cost<<endl; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆回县| 博乐市| 清河县| 策勒县| 桂林市| 东阳市| 界首市| 辉南县| 芦山县| 沙田区| 壶关县| 定远县| 郴州市| 金川县| 沾化县| 莱阳市| 渑池县| 金溪县| 湖口县| 文水县| 梅河口市| 交城县| 融水| 东阿县| 内江市| 宁武县| 建水县| 海安县| 信宜市| 桃园县| 鲜城| 乐平市| 吴川市| 哈巴河县| 通州市| 饶河县| 盐津县| 海城市| 施秉县| 武乡县| 龙口市|