poj 2349 Arctic Network 最小生成树~~
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11429 | Accepted: 3734 |
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
先把最小生成树的每个结点记住!然后排序即可,求出满足题意的最小值即可
代码:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #define MAX 550 #define INF 1000000000 using namespace std ; struct Point{ int x ,y ; }point[MAX]; double lowCost[MAX] , graph[MAX][MAX] ; bool visited[MAX] ; double prim(int n) { memset(visited,false,sizeof(visited)) ; for(int i = 0 ; i < n ; ++i) { lowCost[i] = graph[0][i] ; } lowCost[0] = INF ; visited[0] = false ; double sum = 0 ; for(int i = 0 ; i < n-1 ; ++i) { int index = 0 ; for(int j = 1 ; j < n ; ++j) { if(!visited[j] && lowCost[index]>lowCost[j]) { index = j ; } } if(index == 0) { break ; } sum += lowCost[index] ; visited[index] = true ; for(int j = 1 ; j < n ; ++j) { if(!visited[j] && lowCost[j]>graph[index][j]) { lowCost[j] = graph[index][j] ; } } } return sum ; } bool cmp(const double &a , const double &b) { return a>b ; } int main() { int n , s , p; scanf("%d",&n); while(n--) { scanf("%d%d",&s,&p) ; for(int i = 0 ; i < p ; ++i) { scanf("%d%d",&point[i].x,&point[i].y) ; } for(int i = 0 ; i < p ; ++i) { for(int j = i+1 ; j < p ; ++j) { graph[i][j] = graph[j][i] = sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)*1.0+(point[i].y-point[j].y)*(point[i].y-point[j].y)*1.0) ; } } double sum = prim(p) ; sort(lowCost+1,lowCost+p,cmp) ; printf("%.2lf\n",lowCost[s]) ; } return 0 ; }
与君共勉
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。