uva_10369_mst

                       Arctic Network

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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

  这道题就是说,MST后,求所有边中的第s大的边。  用一个ans[MAXN]存储加入的边。  刚开始把设置MAXN=150,结果一直wa了6次,我竟然一直没有发现。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=550;
const double INF=999999999.9;
struct Point
{
  double x,y;
}point[MAXN];
bool vis[MAXN];
double cost[MAXN][MAXN];
double lowc[MAXN];
double ans[MAXN];
double get_dis(int i,int j)
{
  return (point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y);
}
int prim(int s,int n)
{
  memset(vis,false,sizeof(vis));
  vis[0]=true;
  int tot=0;
  for(int i=1;i<n;i++)
    lowc[i]=cost[0][i];
  for(int i=1;i<n;i++)
  {
    double minc=INF;
    int p=-1;
    for(int j=0;j<n;j++)
    {
      if(!vis[j]&&lowc[j]<minc)
      {
        minc=lowc[j];
        p=j;
      }

    }
    ans[tot++]=minc;//加入边的同时把边加入ans
    vis[p]=true;
    for(int j=0;j<n;j++)
      if(!vis[j]&&cost[p][j]<lowc[j])
        lowc[j]=cost[p][j];
  }
  sort(ans,ans+tot);
  return ans[tot-s];
}
int main()
{
  int test;
  scanf("%d",&test);
  while(test--)
  {
    int s,n;
    scanf("%d%d",&s,&n);
    for(int i=0;i<n;i++)
    {
      scanf("%lf%lf",&point[i].x,&point[i].y);//double型输入用lf
    }
    for(int i=0;i<n;i++)
    {
      lowc[i]=INF;
      for(int j=0;j<n;j++)
      cost[i][j]=cost[j][i]=get_dis(i,j);
    }
    double ans=prim(s,n);
    ans=sqrt(ans);
    printf("%.2f\n",ans);//double 型输出用f,记得\n
  }
  return 0;
}

				
时间: 2024-11-08 19:37:53

uva_10369_mst的相关文章