Arctic Network poj 2349

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

题意:现有 m 个地点, n 个卫星。  卫星有一个神奇的作用, 作用在于只要某个地方安装有卫星, 不论他们之间距离为多远, 都能够互相接收到彼此间信 号, (这么好的东西当然不会让每个地方都有的), 那么其他地方需要通过收发器 D 来连接。让你求1 - m 个地方在共安装过 n 个卫星后,我们所需要     的 收发器 D 所需的最大距离是多少?

 

*****一开始提交错误仍然是因为%lf和%f,吸取吸取教训。。。

*******问:有人告诉我不能在printf中使用%lf。为什么printf()用%f输出double型,而scanf却用%lf呢?

     答:printf的%f说明符的确既可以输出float型又可以输出double型。根据“默认参数提升”规则(在printf这样的函数的可变参数列表中,不论作用域内有没有原型,都适用这一规则)float型会被提升为double型。因此printf()只会看到双精度数。

(严格地讲,%lf在printf下是未定义的,但是很多系统可能会接受它。要确保可移植性,就要坚持使用%f。)

对于scanf,情况就完全不同了,它接受指针,这里没有类似的类型提升。(通过指针)向float存储和向double存储大不一样,因此,scanf区别%f和%lf。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;

#define maxn 1510
#define oo 0x3f3f3f3f
double maps[maxn][maxn], dist[maxn], p[maxn];
int  v[maxn];
int n, m;

struct node
{
    int x, y;
}s[maxn];

void Init()
{
    int i, j;

    for(i=1; i<=m; i++)
    {
        for(j=1; j<=m; j++)
        {
            if(i==j) maps[i][j]=0;
            else maps[i][j] = maps[j][i] =oo;
        }
    }
}

void Dij()
{

    memset(v, 0, sizeof(v));
    for(int i=1; i<=m; i++)
        dist[i] = maps[1][i];
        v[1] = 1;

    for(int i=1; i<m; i++)
    {
        int index=-1;
         p[i] = 1000000.0;
        for(int j=1; j<=m; j++)
        {
            if(!v[j] && dist[j]<p[i])
            {
                index = j;
                p[i] = dist[j];
            }
        }

        v[index] = 1;

        for(int j=1; j<=m; j++)
        {
            if(!v[j] && dist[j]>maps[index][j])
                dist[j] = maps[index][j];
        }
    }

}
int main()
{
    int T;
    scanf("%d", &T);

    while( T --)
    {
        scanf("%d %d", &n, &m);

        Init();

        for(int i=1; i<=m; i++)
            scanf("%d %d", &s[i].x, &s[i].y);

        for(int i=1; i<m; i++)
        {
            for(int j=i+1; j<=m; j++)
            {
                double l = sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y));
                maps[i][j] = maps[j][i] = min(maps[i][j], l);
            }
        }

        Dij();

        sort(p+1, p+m);

        printf("%.2f\n", p[m-n]);
    }
    return 0;
}



时间: 2024-10-05 05:31:29

Arctic Network poj 2349的相关文章

Arctic Network (poj 2349 最小生成树)

Language: Default Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12397   Accepted: 4053 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different

(最小生成树) Arctic Network -- POJ --2349

链接: http://poj.org/problem?id=2349 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 510; const int INF = 0xfffffff;

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

poj 2349 Arctic Network MST/二分答案

poj 2349 Arctic Network 题目传送 Sol: 方法一: 贪心的想,发现n个点只需要n-1条边即可,求MST即可,再把MST中最大的m-1条边去掉,第m大就是答案. code: #include<string> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define IL inline #define RG register

POJ 2349 Arctic Network

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18447   Accepted: 5829 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network (最小生成树第K大(小)边)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13108   Accepted: 4256 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Prim

Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9973 Accepted: 3302 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technolo

POJ2349:Arctic Network(二分+最小生成树)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28311   Accepted: 8570 题目链接:http://poj.org/problem?id=2349 Description: The Department of National Defence (DND) wishes to connect several northern outposts by a wireless net