-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path165.cpp
More file actions
138 lines (131 loc) · 2.42 KB
/
Copy path165.cpp
File metadata and controls
138 lines (131 loc) · 2.42 KB
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
134
135
136
137
138
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int dn;
int x[2000];
int y[2000];
double dist[20500];
short ia[20500];
short ib[20500];
short father[2000];
double getDist(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}
void print() {
cout << "printing\n";
for (int i = 0; i < dn; i++) {
cout << ia[i] << " " << ib[i] << " ";
cout << setprecision(3);
cout << fixed;
cout << dist[i] << endl;
}
}
bool check(short x, short y) {
short fx = father[x];
short fy = father[y];
while (fx != father[fx]) {
fx = father[fx];
}
while (fy != father[fy]) {
fy = father[fy];
}
father[x] = fx;
father[y] = fy;
if (fx == fy) {
return true;
}
else {
return false;
}
}
void uni(short x, short y) {
if (!check(x, y)) {
short fx = father[x];
short fy = father[y];
while (fx != father[fx]) {
fx = father[fx];
}
while (fy != father[fy]) {
fy = father[fy];
}
father[fx] = father[fy];
}
}
void merge(int s, int t) {
int mid = (s + t) / 2;
if (s < t) {
merge(s, mid);
merge(mid + 1, t);
int i = 0;
int j = mid - s + 1;
short* tempa = new short[t - s + 1];
short* tempb = new short[t - s + 1];
double* temp = new double[t - s + 1];
for (int k = 0; k < t - s + 1; k++) {
temp[k] = dist[s + k];
tempa[k] = ia[s + k];
tempb[k] = ib[s + k];
}
int k = s;
while (k <= t) {
if (j > t - s || (i <= mid - s && temp[i] <= temp[j])) {
dist[k] = temp[i];
ia[k] = tempa[i];
ib[k] = tempb[i];
i++;
k++;
}
else if (i > mid - s || (j <= t - s && temp[i] > temp[j])) {
dist[k] = temp[j];
ia[k] = tempa[j];
ib[k] = tempb[j];
j++;
k++;
}
}
delete[] tempa;
delete[] tempb;
delete[] temp;
}
}
int main() {
int caseNum = 0;
while (1) {
caseNum++;
cin >> n;
if (n == 0) {
break;
}
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
dn = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dist[dn] = getDist(x[i], y[i], x[j], y[j]);
ia[dn] = i;
ib[dn] = j;
dn++;
}
}
merge(0, dn - 1);
for (int i = 0; i < n; i++) {
father[i] = i;
}
for (int i = 0; i < dn; i++) {
uni(ia[i], ib[i]);
if (check(0, 1)) {
cout << "Scenario #" << caseNum << endl;
cout << setprecision(3);
cout << fixed;
cout << "Frog Distance = " << dist[i] << endl;
cout << endl;
break;
}
}
}
return 0;
}