poj2349Arctic Network最小生成树

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

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

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
#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;
}


你可能感兴趣的:(poj2349Arctic Network最小生成树)