zoukankan      html  css  js  c++  java
  • HDU Problem 1875 畅通工程再续 【最小生成树Prim】

    畅通工程再续

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 24033    Accepted Submission(s): 7761

    Problem Description
    相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
     
    Input
    输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
    每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
     
    Output
    每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
     
    Sample Input
    2 2 10 10 20 20 3 1 1 2 2 1000 1000
     
    Sample Output
    1414.2 oh!
     
    Author
    8600
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1879 1301 1162 3371 1198 
     
    同样的最小生成树,但是这个对数据有要求,要是两个岛不能相连,用一个最大值标记两个岛之间的距离,检查答案是否大于这个最大值,如果大于这个值,就说明这条边必须要用到,这时候路就建不成了。
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #define MAXN 10005
    using namespace std;
    const int INF = 1e9;
    struct node{
        double x, y;
    }loc[MAXN];
    int n, m, sum = 0;
    bool vis[MAXN];
    double cost[MAXN][MAXN], mincost[MAXN];
    double prim() {
        double ans = 0; memset(vis, false, sizeof(vis));
        for (int i = 0; i <= n; i++) mincost[i] = INF;
        mincost[0] = 0; sum = 0;
        while (true) {
            int v = -1;
            for (int u = 0; u < n; u++) {
                if (!vis[u] && (v == -1 || mincost[v] > mincost[u]))
                    v = u;
            }
            if (v == -1) break;
            vis[v] = true; ans += mincost[v];
            for (int u = 0; u < n; u++) {
                mincost[u] = min(mincost[u], cost[u][v]);
            }
        }
        return ans;
    }
    double dis(node x, node y) {
        return sqrt((x.x - y.x)*(x.x - y.x) +(x.y - y.y)*(x.y - y.y));
    }
    int main() {
        int t; scanf("%d", &t);
        while (t--) {
            scanf("%d", &n);
            for (int i = 0; i < n; i++) {
                scanf("%lf%lf", &loc[i].x, &loc[i].y);
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    double tem = dis(loc[i], loc[j]);
                    //用最大值标记
                    if (tem > 1000 || tem < 10) cost[i][j] = INF;
                    else cost[i][j] = tem;
                }
            }
            double ans = prim();
             if (ans < INF) printf("%.1lf
    ", ans*100);
            else printf("oh!
    ");
        }
        return 0;
    }

  • 相关阅读:
    Bash Shell 快捷键
    vector-swap
    vector-swap
    vector-size
    Android网络篇
    应用 Valgrind 发现 Linux 程序的内存问题
    树莓派初学者?先做做这十个项目吧
    树莓派 (为学习计算机编程教育设计的一种微型电脑)
    微软windows10 IOT支持PI3之后,树莓派3更是将获得Android官方原生支持
    Linux基金会宣布JS Foundation基金会成立 前身为jQuery团队
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770866.html
Copyright © 2011-2022 走看看