poj 2349 Arctic Network prim算法做

来源:互联网 发布:淘宝客服聊天软件 编辑:程序博客网 时间:2024/06/10 14:09

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

Arctic Network
Time Limit: 2000MS
Memory Limit: 65536KTotal 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

12 40 1000 3000 600150 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 999999999using 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;}


原创粉丝点击