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

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

hdu1233-還是暢通工程(Kruskal)

2019-11-11 04:04:45
字體:
來源:轉載
供稿:網友

還是暢通工程 某省調查鄉村交通狀況,得到的統計表中列出了任意兩村莊間的距離。省政府“暢通工程”的目標是使全省任何兩個村莊間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可),并要求鋪設的公路總長度為最小。請計算最小的公路總長度。 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; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰县| 南召县| 西林县| 怀仁县| 无极县| 桂阳县| 本溪| 山西省| 红桥区| 清丰县| 子长县| 新和县| 新蔡县| 郯城县| 南澳县| 崇阳县| 黑河市| 湘潭县| 丰城市| 岐山县| 海伦市| 随州市| 武山县| 蕉岭县| 岐山县| 武邑县| 文安县| 临沂市| 集安市| 秦皇岛市| 平山县| 沭阳县| 江陵县| 邢台市| 永胜县| 调兵山市| 南川市| 富源县| 灵山县| 肥城市| 龙岩市|