poj 2349 Arctic Network 最小生成树prim算法
Arctic Network
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6645 | Accepted: 2270 |
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
Source
该题题意不好理解,关键是在satellite channel上,这s个satellite channel是放在点上的,而不是边上。所以用prim算法求最小生成树,然后将其中最大的s-1条边去掉,剩下的最大的就是结果,即对生成树中的n-1条边进行排序,取第(n-1)-(m-1)条边。
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 8 const int MAX = 505; 9 const int INF = 0x7ffffff; 10 struct node1 11 { 12 int x; 13 int y; 14 }point[MAX]; 15 16 double map[MAX][MAX]; 17 double dist[MAX]; 18 int vis[MAX]; 19 20 int n,m; 21 double ans[MAX]; 22 23 void prim() 24 { 25 int i,j,k; 26 double min; 27 for (i=0; i<n; i++) 28 { 29 dist[i] = map[0][i]; 30 vis[i] = 0; 31 } 32 33 vis[0] = 1; 34 dist[0] = 0; 35 36 for (i=0; i<n-1; i++) 37 { 38 k = -1; 39 min = INF; 40 for (j=0; j<n; j++) 41 { 42 if (!vis[j] && min>dist[j]) 43 { 44 min = dist[j]; 45 k = j; 46 } 47 } 48 49 if (k == -1) break; 50 ans[i] = min; 51 vis[k] = 1; 52 53 for (j=0; j<n; j++) 54 { 55 if (!vis[j] && dist[j]>map[k][j]) 56 dist[j] = map[k][j]; 57 } 58 } 59 } 60 61 int main() 62 { 63 // freopen("in.txt", "r", stdin); 64 int i,j; 65 int T; 66 scanf("%d",&T); 67 while (T--) 68 { 69 scanf("%d %d",&m,&n); 70 for (i=0; i<n; i++) 71 { 72 scanf("%d %d",&point[i].x,&point[i].y); 73 for (j=0; j<i; j++) 74 { 75 map[i][j] = map[j][i] = sqrt(0.0+(point[i].x-point[j].x)*(point[i].x-point[j].x) 76 + (point[i].y-point[j].y)*(point[i].y-point[j].y)); 77 } 78 } 79 prim(); 80 sort(ans,ans+n-1); 81 printf("%.2f\n",ans[n-m-1]); 82 } 83 return 0; 84 }
posted on 2013-01-21 16:17 shijianupc 阅读(197) 评论(0) 收藏 举报

浙公网安备 33010602011771号