poj2349Arctic Network最小生成树

来源:互联网 发布:淘宝如何退保证金 编辑:程序博客网 时间:2024/05/18 07:25

开始是读错题,其实题意只是要顾及临边,即每次加入到集合的那个数本身,排个序就好了==可是为什么cin 改成scanf就不对==

还有就是c++中格式输出的写法== 刚考完就忘了 T^T还好意思嘚瑟自己成绩吗     --->_--->

vjudge上的中文叙述

Description

国防部(DND)要用无线网络连接北部几个哨所。两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道。
任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置。同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率。功率越大,D也越大,但成本更高。出于采购和维修的方便,所有哨所的收发器必须是相同的;那就是说,D值对每一个哨所相同。
 
你的任务是确定收发器的D的最小值。每对哨所间至少要有一条通信线路(直接或间接)。

Input

输入的第一行是测试数据的数量N。
每组测试数据的第一行包含卫星频道的数量S(1 < = S < = 100)和哨所的数量P(S < P < = 500)。接下来的P行,给出以公里为单位的每个哨所的坐标(x,y)(坐标为0到10000之间的整数)。

Output

对于每组测试数据,输出一行,输出收发器的D的最小值。精确到小数点后两位。

Sample Input

12 40 1000 3000 600150 750

Sample Output

212.13

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
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int flag1=0;int n,s,p;double sum;double arr_list[504][504];double d[503];struct Edge{     int point;     double lowcost;     int flag;};Edge edge[505];bool cmp(double a,double b){     return a>b;}struct Point{     double x,y;}point[502];void prim(int p){     int i,j,k=1;     double min,sum2=0;     j=1;     for(i=1;i<=p;i++)     {          if(i!=j)          {               edge[i].point=j;               edge[i].lowcost=arr_list[j][i];               edge[i].flag=0;          }     }     edge[j].lowcost=0;     edge[j].flag=1;     int l=0;     for(i=1;i<p;i++)     {          min=65535000;          for(j=2;j<=p;j++)          {               if(edge[j].flag==0&&edge[j].lowcost<min)               {                    k=j;                    min=edge[j].lowcost;               }          }          d[l++]=arr_list[k][edge[k].point];          //sum2+=min;          edge[k].flag=1;          for(j=2;j<=p;j++)          {               if(edge[j].flag==0&&arr_list[k][j]<edge[j].lowcost)               {                    edge[j].point=k;                    edge[j].lowcost=arr_list[k][j];               }          }     }}int main(){      //  freopen("cin.txt","r",stdin);         cin>>n;         while(n--)         {               cin>>s>>p;               for(int i=1;i<=p;i++)               {                    cin>>point[i].x>>point[i].y;                    arr_list[i][i]=65535000;               }               for(int i=1;i<p;i++)               {                    for(int j=i+1;j<=p;j++)                    {                         arr_list[i][j]=sqrt(pow((point[i].x-point[j].x),2)+pow((point[i].y-point[j].y),2));                         arr_list[j][i]=arr_list[i][j];                   //cout<<arr_list[i][j]<<endl;                    }               }               prim(p);               sort(d,d+p-1,cmp);               cout.setf(ios::fixed);//保留两位小数               cout.precision(2);               cout<<d[s-1]<<endl;         }    return 0;}


0 0
原创粉丝点击