POJ 2349————最小生成树
Arctic Network
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10281 | Accepted: 3394 |
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
这个题目一开始没大看懂,后来明白。
要求简单点说是这样。
求最小生成树的第S-1大的边(也就是题目中的D)
分析:
如果有卫星,那么城市之间的距离完全可以看成是0,不在这个D的范围之内。
最简便的做法就是先求出最小生成树,然后把最小生成树的边按照从大到小的顺序排好,输出第s-1大的边即可。
POJ亲测37MS
<span style="font-family:Microsoft YaHei;font-size:14px;">#include<string.h> #include<stdio.h> #include<math.h> #include<stdlib.h> #define maxcost 100000 #define maxn 505 struct Node{ double x,y; }; struct Node dis[maxn]; double G[maxn][maxn],mindist[maxn],sum[maxn]; int intree[maxn],S,P; int cmp(const void*a,const void*b) { return ((*(double*)b > *(double*)a > 0) ? 1 : -1); } double distance(double x1,double y1,double x2,double y2 ) { return sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)); } void prim() { double temp; int count = 0,node; for(int i = 1 ; i <= P ; i++) mindist[i] = G[1][i]; intree[1] = 1; //默认第一个结点进入最小生成树 for(int i = 2 ; i <= P ; i++) { temp = maxcost; for(int j = 1 ; j <= P ; j++) { if(temp > mindist[j] && !intree[j]) { temp = mindist[j]; node = j; } } intree[node] = 1; sum[count++] = temp;/*加入的是第几条边*/ for(int j = 1 ; j <= P ; j++) if(mindist[j] > G[node][j] && !intree[j]) mindist[j] = G[node][j]; } qsort(sum,count,sizeof(sum[0]),cmp); printf("%.2lf\n",sum[S-1]); } int main() { int test_case,node; double temp; scanf("%d",&test_case); while(test_case--) { memset(G,0,sizeof(G)); memset(intree,0,sizeof(intree)); memset(sum,0,sizeof(sum)); memset(mindist,0,sizeof(mindist)); scanf("%d%d",&S,&P); for(int i = 1 ; i <= P ; i++) scanf("%lf%lf",&dis[i].x,&dis[i].y); for(int i = 1 ; i <= P ; i++) for(int j = 1 ; j <= P ; j++) G[i][j] = G[j][i] = distance(dis[i].x,dis[i].y,dis[j].x,dis[j].y); prim(); } return 0; }</span>