HDU 1162 Eddy's picture
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?InputThe first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.
Input contains multiple test cases. Process to the end of file.
OutputYour program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input3 1.0 1.0 2.0 2.0 2.0 4.0Sample Output
3.41
普普通通的最小生成树,用克鲁斯卡尔算法,注意每条边的权值要自己算
克鲁斯卡尔:从小到大排列边,然后用并查集链接,已连接的就不再链接
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<cstdlib> #include<cmath> #include<queue> using namespace std; typedef long long ll; #define Inf 99999999 #define Maxn 10005 #define Naxn 85 int f[Maxn], n, m; struct Node { int a, b; double s; }; void init() { for (int i = 0; i < n; i++) { f[i] = i; } } int find(int t) { if (f[t] != t) { f[t] = find(f[t]); } return f[t]; } bool marge(int a, int b) { int x = find(a); int y = find(b); if (x != y) { f[y] = f[x]; return true; } return false; } bool cmp(Node a, Node b) { return a.s < b.s; } int main() { while (~scanf("%d",&n)) { init(); double fx[Maxn] = { 0 }; double fy[Maxn] = { 0 }; for (int i = 0; i < n; i++) { cin >> fx[i] >> fy[i]; } Node t[Maxn]; m = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { t[m].a = i; t[m].b = j; t[m].s = sqrt((pow(fx[i] - fx[j], 2)) + (pow(fy[i] - fy[j], 2))); m++; } } double sum = 0; sort(t, t + m, cmp); for (int i = 0; i < m; i++) { if (marge(t[i].a, t[i].b)) { sum += t[i].s; } } printf("%.2lf\n", sum); } return 0; }
一天不做题,浑身难受

浙公网安备 33010602011771号