• HDU4081 Qin Shi Huang's National Road System


    #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
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    考研岁月
    sklearn 翻译笔记:KNeighborsClassifier
    网站不让复制的办法
    关于VMware Workstation 15 Player 虚拟机安装Windows系统却无法安装vmware tools的解决
    今天真是太傻了
    GoogleHacking相关技巧
    判断ARP欺骗
    Linux各个文件及其含义
    小白的开始—转一篇Linux系统入门的文章
    课时39.细线表格(理解)
  • 原文地址:https://www.cnblogs.com/albert-biu/p/11449557.html
Copyright © 2020-2023  润新知