標籤:
Arctic Network
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 16968 |
|
Accepted: 5412 |
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
Source
Waterloo local 2002.09.28【題意】有S顆衛星和P個哨所,有衛星的兩個哨所之間可以任意通訊;否則,一個哨所只能和距離它小於等於D的哨所通訊。給出衛星的數量和P個哨所的座標,求D的最小值。【分析】為了練練Prim。可以先用Prim把邊建好,存起來,然後把選擇的邊從小到大排個序,第(n-p-1)個數就是答案。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#include<functional>#define mod 1000000007#define inf 0x3f3f3f3f#define pi acos(-1.0)using namespace std;typedef long long ll;const int N=2005;const int M=15005;double edg[N][N];double lowcost[N];//記錄未加入樹集合的i離樹集合中元素最小的距離int n,m,t,v;double d[N];bool cmp(double a,double b){return a<b;}struct man{ double x,y;int num;}a[N];double fun(man a,man b){ double cnt=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); return cnt;}void Build(){ for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { edg[i][j]=edg[j][i]=fun(a[i],a[j]); } }}void prim(){ double sum=0;lowcost[0]=-1; for(int i=1;i<n;i++){ lowcost[i]=edg[0][i]; } for(int i=1;i<n;i++){ double minn=inf;int k; for(int j=0;j<n;j++){ if(lowcost[j]!=-1&&lowcost[j]<minn){ k=j;minn=lowcost[j]; } } sum+=minn; d[v++]=minn; lowcost[k]=-1; for(int j=0;j<n;j++){ lowcost[j]=min(lowcost[j],edg[k][j]); } } sort(d,d+v,cmp); printf("%.2lf\n",d[n-m-1]);}int main() { scanf("%d",&t); while(t--) { memset(edg,0,sizeof(edg)); memset(lowcost,0,sizeof(lowcost)); memset(d,0,sizeof(d)); v=0; scanf("%d%d",&m,&n); for(int i=0;i<n;i++){ scanf("%lf%lf",&a[i].x,&a[i].y); a[i].num=i; } Build(); prim(); } return 0;}View Code
POJ2032 Arctic Network(Prim)