还是畅通project
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32795 Accepted Submission(s): 14769 Problem Description
某省调查乡村交通状况。得到的统计表中列出了随意两村庄间的距离。省政府“畅通project”的目标是使全省不论什么两个村庄间都能够实现公路交通(但不一定有直接的公路相连。仅仅要能间接通过公路可达就可以),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
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 Huge input, scanf is recommended.
Source
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233
解题思路:裸的最小生成树,克鲁斯卡尔,边权值排序。
代码例如以下:
#include#include #include using namespace std;#define inf 1e9const int maxn=10005;int fa[maxn];struct EG{ int x,y,w;}eg[maxn];void get_fa(){ for(int i=0;i<=maxn;i++) fa[i]=i;}int find(int x){ return x==fa[x]?x:find(fa[x]);}void Union(int a,int b){ int a1=find(a); int b1=find(b); if(a1!=b1) fa[a1]=b1;}int cmp(EG a,EG b){ return a.w