hdu Eddy's picture (最小生成树)

简介:
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1162

/*
***********************************************************************/ /* hdu Eddy's picture 最小生成树 题目大意:在坐标轴上存在n个点,求这n个点组成的最小图,使得图的总路径最短 解题思路:求最小生成树 */ /************************************************************************/ #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> typedef struct { double x; double y; }P; const int N = 101; P p[N]; double map[N][N]; int mark[N]; int n,q,i,j; //返回两个点的距离 double distance(P p1,P p2) { return sqrt(pow(p1.x - p2.x,2)+pow(p1.y-p2.y,2)); } double Prim() { double sum = 0; memset(mark,0,sizeof(mark)); int k,t = n; while(--t) { double min = 100000; for (i = 1; i < n; i++) { if(mark[i] != 1 && min > map[0][i]) { min = map[0][i]; k = i; } } mark[k] = 1; sum += min; for( j = 1; j < n; j++) { if(mark[j]!=1 && map[k][j] < map[0][j]) map[0][j] = map[k][j]; } } return sum; } int main() { while(scanf("%d",&n) != EOF) { for(i = 0; i < n; i++) { scanf("%lf%lf",&(p[i].x),&(p[i].y)); } for (i = 0; i < n; i++) { for(j = i; j < n; j++) { if(i==j) map[i][j] = map[j][i] = 0; else map[i][j] = map[j][i] = distance(p[i],p[j]); } } printf("%.2f\n",Prim()); } return 0; }
复制代码

 







本文转自NewPanderKing51CTO博客,原文链接: http://www.cnblogs.com/newpanderking/p/3248604.html,如需转载请自行联系原作者


相关文章
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
75 0
|
测试技术
HDU-1026,Ignatius and the Princess I(BFS+打印路径)
HDU-1026,Ignatius and the Princess I(BFS+打印路径)
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
HDU-1029,Ignatius and the Princess IV
HDU-1029,Ignatius and the Princess IV
hdu-1098 Ignatius's puzzle(费马小定理)
hdu-1098 Ignatius's puzzle(费马小定理)
126 0
hdu-1098 Ignatius's puzzle(费马小定理)
|
Java C语言
HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
119 0
HDOJ/HDU 1372 Knight Moves(经典BFS)
HDOJ/HDU 1372 Knight Moves(经典BFS)
95 0
|
测试技术
Knight Moves(骑士移动) (bfs) POJ-2243
题目:Knight Moves (骑士移动)