Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24020 | Accepted: 7388 |
Description
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
Output
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
Code
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 1010
#define INF 0x3f3f3f3f
using namespace std;
double map[N][N];
double dis[N];
int vis[N];
int num;
struct node
{
double x, y;
}s[N];
double getDist(int i,int j)
{
return sqrt( (s[i].x - s[j].x) * (s[i].x - s[j].x) + (s[i].y - s[j].y) * (s[i].y - s[j].y) );
}
void prim(int n)
{
int i,j,pos,k=0;
double min;
double ans[N];
for(i=0; i<n; i++)
{
dis[i] = map[1][i];
vis[i] = 0;
}
vis[1] = 1;
for(i=0; i<n; i++)
{
min = INF * 1.0;
for(j=0; j<n; j++)
{
if(!vis[j] && dis[j] < min)
{
min = dis[j];
pos = j;
}
}
if(min != INF * 1.0)
{
///保存每條路徑
ans[k++] = min;
vis[pos] = 1;
for(j=0; j<n; j++)
{
if(!vis[j] && dis[j] > map[pos][j])
{
dis[j] = map[pos][j];
}
}
}
}
///排序後去掉num條路徑,即建立衛星通道
sort(ans,ans+k);
///輸出最長的路徑,就是收發器的最小D值
printf("%.2f\n",ans[k-num]);
}
int main()
{
int t,i,j,n;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&num,&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i == j)
map[i][j] = 0;
else
map[i][j] = INF;
}
}
for(i=0;i<n;i++)
{
scanf("%lf %lf",&s[i].x,&s[i].y);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
map[i][j] = getDist(i,j);
}
}
prim(n);
}
return 0;
}
反思:
題目大意:國防部給北極前哨建設通信網絡,有兩種通信技術,一種是可以無視距離的衛星頻道,另一種是距離小於D的無線電,現在要求能連通網絡的最小距離D。第一行是測試數據組數t,每組數據第一行是衛星通道數和前哨數量,然後給出每個前哨的x,y座標。最小生成樹prim做法,初始化圖後通過求每點的距離存圖,類似POJ-2031,不過這個是二維~因爲要求的是最小距離D,不用求路徑的權值總和,用一個數組ans存每次的路徑長度即可,最後從小到大排序,最長的num個路徑使用衛星頻道進行通信,不需要無線電,所以此時ans數組的最後一個就是收發器所需的最小距離D。