zoukankan      html  css  js  c++  java
  • poj2349,最小生成树!

    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
    题目看起来简单,花了一个多小时才改对,主要是s的问题没有解决!!
    用一个数组保存,然后sort一下,求num-s就行了
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const double inf=1e20;
    double d[600],cost[600][600],ans[600];
    int n,s;
    struct node{
       double x,y;
    }e[600];
    double dis(node a,node b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    void prim()
    {
        bool vis[600];
        for(int i=0;i<=n;i++)
        {
            vis[i]=0;
            d[i]=inf;
        }
        d[0]=0;
        int num=0;
        while(1){
            int v=-1;
            for(int i=0;i<n;i++)
            {
                if(!vis[i]&&(v==-1||d[i]<d[v]))
                    v=i;
            }
            if(v==-1)break;
            ans[num++]=d[v];
            vis[v]=1;
            for(int i=0;i<n;i++)
            {
                d[i]=min(d[i],cost[i][v]);
            }
        }
        sort(ans,ans+num);
        printf("%.2f ",ans[num-s]);
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&s,&n);
            for(int i=0;i<n;i++)
            {
                scanf("%lf%lf",&e[i].x,&e[i].y);
            }
            for(int i=0;i<n;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                   cost[i][j]=cost[j][i]=dis(e[i],e[j]);
                }
                cost[i][i]=0;
            }
            prim();
        }
        return 0;
    }
  • 相关阅读:
    IDEA debug时特慢 Method breakpoints may dramatically slow down debugging
    docker构建镜像
    ubuntu 挂载硬盘
    python 的 flask 、django 、tornado 、sanic
    scrapy实战之scrapyrt的使用
    scrapy框架集成http
    python3之Splash
    CentOS7安装PostgreSQL9.6(图文详细操作)
    替代Navicat的数据库操作工具DBeaver
    CentOS 7 安装 Graylog
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6481222.html
Copyright © 2011-2022 走看看