POJ 2349 Arctic Network
Arctic Network
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20823 | Accepted: 6427 |
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.
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
题意:有n个点给出坐标,点和点之间可以用无线电或者卫星通信,每个点都有无线电收发器可进行无线电通信,但是只有m个点有卫星通信功能。
卫星通信的距离可以无限大,但无线电通信的距离不能超过D,超过D的部分将使通信费用增加。要使通信费用最少。
其实就是求一次最小生成树,m个点有卫星通信,那么就会有m-1条边的通信距离无限大,其实就是这m-1条边不用计算费用。而剩下的边中,
找出最大边作为D值,这样剩下的所有的边都不会大于D,那么不会增加通信费用。在构建MST过程中保存下所有的边权值,然后按升序排序,
除掉最后的m-1条边(也就是最大的m-1条边,这些边用卫星通信),最大的那条就是D;
//最小生成树 #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 0x3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; double g[550][550]; double dis[1550]; int vis[550],t,n,m; struct Node { double x; double y; }node[550]; double dist(Node a,Node b) { return (double)sqrt(((a.x-b.x)*(a.x-b.x)*1.0)+((a.y-b.y)*(a.y-b.y)*1.0)); } bool cmp(double x,double y) { return x>y; } double prime() { for(int i=1;i<=m;i++) { dis[i]=g[1][i]; vis[i]=0; } vis[1]=1; int v; double minn; for(int i=1;i<=m;i++) { minn=INF;v=0; for(int j=1;j<=m;j++) { if(!vis[j] && minn>dis[j]) { v=j; minn=dis[j]; } } vis[v]=1; for(int j=1;j<=m;j++) { if(!vis[j]) dis[j]=min(dis[j],g[v][j]); } } sort(dis+1,dis+m+1,cmp); return dis[n]; } int main() { scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%lf %lf",&node[i].x,&node[i].y); } for(int i=1;i<=m;i++) { for(int j=1;j<=i;j++) { g[i][j]=g[j][i]=(double)dist(node[i],node[j]); } } printf("%.2f\n",prime()); } return 0; }