Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11429 | Accepted: 3734 |
Description
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 ; }
与君共勉