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

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

hdu1233-還是暢通工程(Kruskal)

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

還是暢通工程 某省調查鄉村交通狀況,得到的統計表中列出了任意兩村莊間的距離。省政府“暢通工程”的目標是使全省任何兩個村莊間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可),并要求鋪設的公路總長度為最小。請計算最小的公路總長度。 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; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜都市| 太和县| 宣化县| 贵州省| 马山县| 建平县| 海伦市| 楚雄市| 临西县| 邵东县| 镇江市| 郧西县| 无极县| 荣成市| SHOW| 称多县| 龙陵县| 惠水县| 沙河市| 江都市| 铜山县| 宁波市| 天水市| 治多县| 樟树市| 南乐县| 扎赉特旗| 射洪县| 晋宁县| 明星| 宜兰县| 天长市| 阿勒泰市| 东乡族自治县| 镇平县| 海林市| 桂林市| 建平县| 建瓯市| 九寨沟县| 海南省|