poj2349——Arctic Network(最小生成树+prim)

来源:互联网 发布:奇门择日软件 编辑:程序博客网 时间:2024/06/05 16:38

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

给出p个哨所的坐标,哨所之间的通讯距离不能小于d,或者可以直接用卫星通讯,这样就不需要考虑距离,求最小的d。
如果没有卫星通讯,那就是个很裸的最小生成树,加了卫星后,意味着最小生成树中最长的s-1条边可以去掉,然后剩下的最长的边就是d。
思路就是记录求最小生成树过程中产生的边,排序然后输出第s大的边。
一开始数组开了200居然超时,好气啊

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 2005#define Mod 10001using namespace std;struct Node{    double x,y;};Node p[MAXN];int n,vis[MAXN],ans;double map[MAXN][MAXN],dis[MAXN];double getdis(int a,int b){    double x=p[a].x-p[b].x,y=p[a].y-p[b].y;    return sqrt(x*x+y*y);}double m[MAXN];void prim(){    int i,j,pos;    double min;    ans=0;    memset(vis,0,sizeof(vis));    vis[1]=1,pos=1;    for(i=1; i<=n; ++i)        if(i!=pos)            dis[i]=map[pos][i];    for(i=1; i<n; ++i)    {        min=INF;        for(j=1; j<=n; ++j)            if(!vis[j]&&dis[j]<min)            {                min=dis[j];                pos=j;            }        //ans+=min;        m[ans++]=min;        vis[pos]=1;        for(j=1; j<=n; ++j)            if(!vis[j]&&dis[j]>map[pos][j])                dis[j]=map[pos][j];    }    //return ans;}int main(){    int s,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&s,&n);        for(int i=1; i<=n; ++i)            scanf("%lf%lf",&p[i].x,&p[i].y);        for(int i=1; i<=n; ++i)            for(int j=1; j<=n; ++j)                map[i][j]=getdis(i,j);        prim();        sort(m,m+ans);        printf("%.2lf\n",m[n-s-1]);    }    return 0;}
0 0
原创粉丝点击