程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> uva 10228 - Star not a Tree?(模擬退火)

uva 10228 - Star not a Tree?(模擬退火)

編輯:C++入門知識

uva 10228 - Star not a Tree?(模擬退火)


題目鏈接:uva 10228 - Star not a Tree?

題目大意:給定若干個點,求費馬點(距離所有點的距離和最小的點)

解題思路:模擬退火算法,每次向周圍嘗試性的移動步長,如果發現更優點,則轉移。每次操作之後減少步長後做同樣的操作,直到步長小於指定精度。

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
const int maxn = 105;
const int MOD = 1e4+1;
const double eps = 1e-9;
const double INF = 0x3f3f3f3f3f3f3f3f;
const int dir[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };

struct point {
    double x, y;
    point (double x = 0, double y = 0) {
        this->x = x;
        this->y = y;
    }
}p[maxn];
int N;

double distance (point u) {
    double ret = 0;
    for (int i = 0; i < N; i++) {
        double dx = u.x - p[i].x;
        double dy = u.y - p[i].y;
        ret += sqrt(dx * dx + dy * dy);
    }
    return ret;
}

double solve () {
    int ti = 10;
    double ret = INF, r = 0.9;

    srand(time(NULL));

    while (ti--) {
        point u(rand() % MOD, rand() % MOD);
        double step = 1e4;
        double dis = distance(u);

        while (step > eps) {
            point v = u;

            for (int i = 0; i < 8; i++) {
                point tr(u.x + dir[i][0] * step, u.y + dir[i][1] * step);
                double tmpd = distance(tr);

                if (tmpd < dis) {
                    dis = tmpd;
                    v = tr;
                }
            }

            u = v;
            step *= r;
            ret = min(ret, dis);
        }
    }
    return ret;
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d", &N);
        for (int i = 0; i < N; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        printf("%.0lf\n", solve());

        if (cas)
            printf("\n");
    }
    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved