-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEddy's picture-并查集+最小生成树.cpp
133 lines (119 loc) · 3.41 KB
/
Eddy's picture-并查集+最小生成树.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <math.h>
using namespace std;
struct node {
int s,t;
double l;
};
int f[500];
double sum;
int cmp(const void *a,const void *b)
{
struct node *c=(struct node *)a;
struct node *d=(struct node *)b;
if (c->l > d->l)
return 1;
else return -1;
}
int find(int x)
{
return f[x]==x ? x : find(f[x]);
}
void Union(int x,int y,double w)
{
if (x==y)
return;
f[y]=x;
sum+=w;
}
int main()
{
int n;
while (~scanf("%d",&n)) {
int i,j,cnt=0;
node path[10100];
double x[110],y[110];
memset(f, 0, sizeof(f));
for (i=1; i<=n; i++)
cin >> x[i] >> y[i];
for (i=1; i<=n; i++)
for (j=i+1; j<=n; j++) {
path[cnt].s=i;
path[cnt].t=j;
path[cnt++].l=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
qsort(path, cnt, sizeof(path[0]), cmp);
for (i=1; i<=n; i++)
f[i]=i;
sum=0.0;
for (i=0; i<cnt; i++)
Union(find(path[i].s),find(path[i].t),path[i].l);
printf("%.2f\n",sum);
}
return 0;
}
/*
#include <iostream>
#include <math.h>
using namespace std;
const int inf=999999;
double dis[110][110];
int n;
double prim()
{
int i,j,lab,v[110]={0};
double min,lowcost[110],ans=0;
for (i=0; i<n; i++)
lowcost[i]=dis[0][i];
v[0]=1;
for (i=1; i<n; i++) {
min=inf;
lab=0;
for (j=0; j<n; j++)
if (!v[j]&&lowcost[j]<min) {
min=lowcost[j];
lab=j;
}
ans+=min;
v[lab]=1;
for (j=0; j<n; j++)
if(!v[j]&&lowcost[j]>dis[lab][j])
lowcost[j]=dis[lab][j];
}
return ans;
}
int main()
{
while (~scanf("%d",&n)) {
int i,j;
double x[110],y[110];
for (i=0; i<n; i++)
for (j=0; j<n; j++)
dis[i][j]=inf;
for (i=0; i<n; i++)
cin >> x[i] >> y[i];
for (i=0; i<n; i++)
for (j=0; j<n; j++)
dis[i][j]=dis[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
printf("%.2lf\n",prim());
}
return 0;
}
*/
/*
Problem Description
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?
Input
The 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.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41
*/