Kruskal算法解决HDU 1162
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1162
题目大意我就不说了,很容易看明白。
直接上代码吧,还是,只要你会Kruskal,这个题目没有什么难度。
View Code
#include<iostream> #include<algorithm> #include<cmath> #include<string> #include<cstring> using namespace std; int father[10005], Enums, Count; double MinL; struct Point{ int begin; int end; double Dis; }P[10005]; int Find(int k){ return father[k]==k?k:father[k]=Find(father[k]); } int Cmp(Point a, Point b){ return a.Dis<b.Dis; } double Kruskal(){ for(int i=0; i<Count; i++){ father[i] = i; } for(int i=0; i<Count; i++){ int a = Find(P[i].begin); int b = Find(P[i].end); if(a!=b){ father[b] = a; MinL += P[i].Dis; } } return MinL; } void Init(){ int N; double x[100], y[100]; while(cin>>N){ for(int i=0; i<N; i++){ cin>>x[i]>>y[i]; } MinL = 0.0; Count=0; for(int i=0; i<N; i++){ for(int j=i+1; j<N; j++){ P[Count].begin = i; P[Count].end = j; P[Count].Dis = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); Count++; } } sort(P, P+Count, Cmp); printf("%.2lf\n", Kruskal()); } } int main(){ Init(); }
posted on 2012-04-24 20:59 More study needed. 阅读(220) 评论(0) 编辑 收藏 举报