zoukankan      html  css  js  c++  java
  • (最小生成树) Arctic Network -- POJ --2349

    链接:

    http://poj.org/problem?id=2349

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 13715   Accepted: 4424

    Description

    The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel. 
    Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts. 

    Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

    Input

    The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

    Output

    For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

    Sample Input

    1
    2 4
    0 100
    0 300
    0 600
    150 750
    

    Sample Output

    212.13

    代码:

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int N = 510;
    const int INF = 0xfffffff;
    
    struct node
    {
        int x, y;
    }s[N];
    
    
    int n;
    double ss[N];
    double J[N][N], dist[N];
    bool vis[N];
    
    void Prim()
    {
        int i, j;
        dist[1]=0;
        memset(vis, 0, sizeof(vis));
        vis[1]=1;
    
        for(i=1; i<=n; i++)
            dist[i]=J[1][i];
    
        for(i=1; i<n; i++)
        {
            int index=1;
            double MIN=INF;
            for(j=1; j<=n; j++)
            {
                if(!vis[j] && dist[j]<MIN && dist[j])
                {
                    index=j;
                    MIN=dist[j];
                }
            }
            vis[index]=1;
            ss[index] = MIN;
            for(j=1; j<=n; j++)
            {
                if(!vis[j] && dist[j]>J[index][j])
                    dist[j]=J[index][j];
            }
        }
    }
    
    int main ()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int m, i, j;
    
            scanf("%d%d", &m, &n);
    
            memset(s, 0, sizeof(s));
            memset(ss, 0, sizeof(ss));
    
            for(i=1; i<=n; i++)
                scanf("%d%d", &s[i].x, &s[i].y);
    
            for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                double k=sqrt(1.0*(s[i].x-s[j].x)*(s[i].x-s[j].x)+1.0*(s[i].y-s[j].y)*(s[i].y-s[j].y));
                J[i][j]=J[j][i]=k;
            }
    
            Prim();
    
            sort(ss+1, ss+n+1);
    
            printf("%.2f
    ", ss[n-m+1]);
    
        }
        return 0;
    }
    勿忘初心
  • 相关阅读:
    ajax 笔记--调用WebService实现求两数之和
    多数据之间的连接操作集中几个.NET常用的方法(不完整)
    判断用户是否存在(通过参数来实现)
    我的机子放到公司了
    给同事过生日,我弄菜,给同事买饭,我值班。
    多数据之间的连接操作MSSQL(不完整)
    TreeView连接数据
    字符编码
    计算机基础
    python入门
  • 原文地址:https://www.cnblogs.com/YY56/p/4735098.html
Copyright © 2011-2022 走看看