码迷,mamicode.com
首页 > 其他好文 > 详细

HDU4081 Qin Shi Huang's National Road System

时间:2019-09-03 00:05:25      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:std   ==   turn   ble   cost   nal   names   c++   cos   

#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1100;
const int INF = 147483647;
bool vis[MAXN];
double lowc[MAXN];
int pre[MAXN];
double MAX[MAXN][MAXN];
bool used[MAXN][MAXN];
double x[1010], y[1010], people[1010];
double cost[MAXN][MAXN];

void init() {
    memset(lowc, 0, sizeof(lowc));
    memset(pre, 0, sizeof(pre));
    memset(cost, 0, sizeof(cost));
}

double Prim(int n) {
    double ans = 0;
    memset(vis, false, sizeof(vis));
    memset(MAX, 0, sizeof(MAX));
    memset(used, false, sizeof(used));
    vis[0] = true;
    pre[0] = -1;
    lowc[0] = 0;
    for (int i = 1; i < n; i++) {
        lowc[i] = cost[0][i];
        pre[i] = 0;
    }
    for (int i = 1; i < n; i++) {
        double minc = INF;
        int p = -1;
        for (int j = 0; j < n; j++) {
            if (!vis[j] && minc > lowc[j]) {
                minc = lowc[j];
                p = j;
            }
        }
        if (minc == INF) {
            return -1;
        }
        ans += minc;
        vis[p] = true;
        used[p][pre[p]] = used[pre[p]][p] = true;
        for (int j = 0; j < n; j++) {
            if (vis[j]&&j!=p) {//这里加上j!=p就AC了,目前原因未知
                MAX[j][p] = MAX[p][j] = max(MAX[j][pre[p]], lowc[p]);
            }
            if (!vis[j] && lowc[j] > cost[p][j]) {
                lowc[j] = cost[p][j];
                pre[j] = p;
            }
        }
    }
    return ans;
}

double dist(double xa, double ya, double xb, double yb) {
    return sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb));
}

int main() {
    int _, n;
    scanf("%d", &_);
    while (_--) {
        scanf("%d", &n);
        init();
        for (int i = 0; i < n; i++) {
            scanf("%lf %lf %lf", &x[i], &y[i], &people[i]);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cost[i][j] = cost[j][i] = dist(x[i], y[i], x[j], y[j]);
            }
        }
        double sum = Prim(n), ans = -1;
        //  cout << sum << endl;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) continue;
                if (!used[i][j]) {
                    ans = max(ans, (people[i] + people[j]) / (sum - MAX[i][j]));
                } else {
                    ans = max(ans, (people[i] + people[j]) / (sum - cost[i][j]));
                }
            }
        }
        printf("%.2f\n", ans);
    }
    return 0;
}

HDU4081 Qin Shi Huang's National Road System

标签:std   ==   turn   ble   cost   nal   names   c++   cos   

原文地址:https://www.cnblogs.com/albert-biu/p/11449557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!