HEU 1003 A Star not a Tree?
1
/**************************************
2
Problem: HEU 1003 A Star not a Tree?
3
Time: 0.0040 s
4
Memory: 260 k
5
Accepted Time: 2009-03-25 11:10:32
6
Tips: 别人的再好,也不是自己的!http://hi.baidu.com/novosbirsk/blog/item/75983f138499f825dd54019b%2Ehtml
7
**************************************/
8
#include <cstdio>
9
#include <cmath>
10
#define PRECISION 1e-8
11
#define N 101
12
struct POINT {
13
double x, y;
14
15
POINT(double x, double y) :
16
x(x), y(y) {
17
}
18
19
POINT() :
20
x(0), y(0) {
21
}
22
};
23
POINT plist[N];
24
25
inline double sqr(double x) {
26
return x * x;
27
}
28
29
double pt_distance(const POINT &p1, const POINT &p2) {
30
return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y));
31
}
32
double get_all_dis(const POINT &p, int n) {
33
double ans = 0.0;
34
for (int i = 0; i < n; i++)
35
ans += pt_distance(plist[i], p);
36
return ans;
37
}
38
39
int main() {
40
int i, n;
41
scanf("%d", &n);
42
for (i = 0; i < n; i++)
43
scanf("%lf%lf", &plist[i].x, &plist[i].y);
44
POINT st = plist[0];
45
double step = 100, mind = get_all_dis(st, n);
46
while (step > 0.2) {
47
int ok = 1;
48
while (ok) {
49
POINT tmp, nt;
50
double t;
51
ok = 0,nt = st;
52
tmp = POINT(st.x, st.y + step);
53
t = get_all_dis(tmp, n);
54
if (t < mind)
55
mind = t, ok = 1, nt = tmp;
56
57
tmp = POINT(st.x, st.y - step);
58
t = get_all_dis(tmp, n);
59
if (t < mind)
60
mind = t, ok = 1, nt = tmp;
61
62
tmp = POINT(st.x + step, st.y);
63
t = get_all_dis(tmp, n);
64
if (t < mind)
65
mind = t, ok = 1, nt = tmp;
66
67
tmp = POINT(st.x - step, st.y);
68
t = get_all_dis(tmp, n);
69
if (t < mind)
70
mind = t, ok = 1, nt = tmp;
71
st = nt;
72
}
73
step = step / 2.0;
74
}
75
int ans = (int) (mind + 0.5) * 100 / 100;
76
printf("%d\n", ans);
77
getchar();getchar();
78
return 0;
79
}
80
/**************************************2
Problem: HEU 1003 A Star not a Tree?3
Time: 0.0040 s4
Memory: 260 k 5
Accepted Time: 2009-03-25 11:10:326
Tips: 别人的再好,也不是自己的!http://hi.baidu.com/novosbirsk/blog/item/75983f138499f825dd54019b%2Ehtml7
**************************************/8
#include <cstdio>9
#include <cmath>10
#define PRECISION 1e-811
#define N 10112
struct POINT {13
double x, y;14

15
POINT(double x, double y) :16
x(x), y(y) {17
}18

19
POINT() :20
x(0), y(0) {21
}22
};23
POINT plist[N];24

25
inline double sqr(double x) {26
return x * x;27
}28

29
double pt_distance(const POINT &p1, const POINT &p2) {30
return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y));31
}32
double get_all_dis(const POINT &p, int n) {33
double ans = 0.0;34
for (int i = 0; i < n; i++)35
ans += pt_distance(plist[i], p);36
return ans;37
}38

39
int main() {40
int i, n;41
scanf("%d", &n);42
for (i = 0; i < n; i++)43
scanf("%lf%lf", &plist[i].x, &plist[i].y);44
POINT st = plist[0];45
double step = 100, mind = get_all_dis(st, n);46
while (step > 0.2) {47
int ok = 1;48
while (ok) {49
POINT tmp, nt;50
double t;51
ok = 0,nt = st;52
tmp = POINT(st.x, st.y + step);53
t = get_all_dis(tmp, n);54
if (t < mind)55
mind = t, ok = 1, nt = tmp;56

57
tmp = POINT(st.x, st.y - step);58
t = get_all_dis(tmp, n);59
if (t < mind)60
mind = t, ok = 1, nt = tmp;61

62
tmp = POINT(st.x + step, st.y);63
t = get_all_dis(tmp, n);64
if (t < mind)65
mind = t, ok = 1, nt = tmp;66

67
tmp = POINT(st.x - step, st.y);68
t = get_all_dis(tmp, n);69
if (t < mind)70
mind = t, ok = 1, nt = tmp;71
st = nt;72
}73
step = step / 2.0;74
}75
int ans = (int) (mind + 0.5) * 100 / 100;76
printf("%d\n", ans);77
getchar();getchar();78
return 0;79
}80




x(x), y(y)
浙公网安备 33010602011771号