poj 2349 Arctic Network prim算法做

搞死我了,居然prim里面输入的点不一样,结果不一样。。。

Arctic Network
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 4205
Accepted: 1518

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


题意:有卫星电台的城市之间可以任意联络。没有卫星电台的城市只能和距离小于等于D的城市联络。告诉你卫星电台的个数S,让你求最小的D.
生成最小生成树,去掉最长的S条边后,剩下最长的边就是D.
也就是求最小生成树中第S+1长的边。

直接用prim算法做

代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#define INF 999999999
using namespace std;

int vis[1005],n;
long double map[1005][1005];
long double d[1005];
struct Node
{
    int x;
    int y;
}node[1005];

void prim(int s)
{
    memset(vis,0,sizeof(vis));
    int i,j,m,p;
    for(i=0;i<n;i++)
    {
        d[i]=map[s][i];      //初始化i到s的距离。
    }
    for(i=0;i<n;i++)
    {
        m=INF;
        p=-1;
        for(j=0;j<n;j++)
        {
            if(vis[j]==0 && d[j]<m)    //找出最短距离的点加入集合。
            {
                m=d[j];
                p=j;
            }
        }
        if(p==-1) break;
        vis[p]=1;
        for(j=0;j<n;j++)
        {
            if(vis[j]==0 && map[p][j]<d[j])   //更新集合外面的点到集合的最短距离。
            {
                d[j]=map[p][j];
            }
		
        }
    }
    return ;
}
bool cmp(int i,int j)    //比较函数。
{
	return i>j;
}

int main()
{
    int i,j,m,x,y,t,k,p,q;
    scanf("%d",&k);
	while(k--)
    {
        scanf("%d%d",&p,&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            node[i].x=x;
            node[i].y=y;
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                double yy=node[j].y-node[i].y;
                double xx=node[j].x-node[i].x;
                if(i==j)  map[i][j]=0;
                else
                {
                    map[i][j]=sqrt(yy*yy+xx*xx);
                }
            }
        }
        prim(1);   //之前写的prim(0),错了无数出次,不知道为什么起始点不一样,会WA。。。。。。
       sort(d,d+n,cmp); 
		if(p==0)
			printf("%2.lf\n",d[0]);
		else 
           printf("%.2lf\n",d[p-1]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值