HEU 1003 A Star not a Tree?

 

 1ExpandedBlockStart.gifContractedBlock.gif/**//**************************************
 2Problem: HEU 1003 A Star not a Tree?
 3Time: 0.0040 s
 4Memory: 260 k 
 5Accepted Time: 2009-03-25 11:10:32
 6Tips: 别人的再好,也不是自己的!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
12ExpandedBlockStart.gifContractedBlock.gifstruct POINT {
13    double x, y;
14
15    POINT(double x, double y) :
16ExpandedSubBlockStart.gifContractedSubBlock.gif        x(x), y(y) {
17    }

18
19    POINT() :
20ExpandedSubBlockStart.gifContractedSubBlock.gif        x(0), y(0{
21    }

22}
;
23POINT plist[N];
24
25ExpandedBlockStart.gifContractedBlock.gifinline double sqr(double x) {
26    return x * x;
27}

28
29ExpandedBlockStart.gifContractedBlock.gifdouble pt_distance(const POINT &p1, const POINT &p2) {
30    return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y));
31}

32ExpandedBlockStart.gifContractedBlock.gifdouble 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
39ExpandedBlockStart.gifContractedBlock.gifint 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);
46ExpandedSubBlockStart.gifContractedSubBlock.gif    while (step > 0.2{
47        int ok = 1;
48ExpandedSubBlockStart.gifContractedSubBlock.gif        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

转载于:https://www.cnblogs.com/main/archive/2009/04/02/1427981.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值