http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=975

最小生成树。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. #define maxn 1000
  6. using namespace std;
  7. const double inf=(double)(<<);
  8.  
  9. int t,n;
  10. struct node
  11. {
  12. double x,y;
  13. }p[maxn];
  14. bool vis[maxn];
  15. double dis[maxn];
  16. double g[maxn][maxn];
  17. double ans;
  18.  
  19. double sqr(double x)
  20. {
  21. return x*x;
  22. }
  23.  
  24. void prim()
  25. {
  26. memset(vis,false,sizeof(vis));
  27. for(int i=; i<=n; i++) dis[i]=g[][i];
  28. dis[]=;
  29. vis[]=true;
  30. for(int i=; i<n; i++)
  31. {
  32. double m=inf;
  33. int x;
  34. for(int y=; y<=n; y++) if(!vis[y]&&dis[y]<m) m=dis[x=y];
  35. ans+=m;
  36. vis[x]=true;
  37. for(int y=; y<=n; y++) if(!vis[y]&&dis[y]>g[x][y]) dis[y]=g[x][y];
  38. }
  39. }
  40.  
  41. int main()
  42. {
  43. scanf("%d",&t);
  44. while(t--)
  45. {
  46. scanf("%d",&n);
  47. for(int i=; i<=n; i++)
  48. {
  49. scanf("%lf%lf",&p[i].x,&p[i].y);
  50. }
  51. for(int i=; i<=n; i++)
  52. {
  53. for(int j=i; j<=n; j++)
  54. {
  55. if(i==j) g[i][j]=;
  56. else
  57. g[i][j]=g[j][i]=sqrt(sqr(p[i].x-p[j].x)+sqr(p[i].y-p[j].y));
  58. }
  59. }
  60. ans=;
  61. prim();
  62. printf("%.2lf\n",ans);
  63. if(t) printf("\n");
  64. }
  65. return ;
  66. }

uva 10034 Problem A: Freckles的更多相关文章

  1. UVA 10034 Freckles 最小生成树

    虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...

  2. UVa 10034 - Freckles

    题目大意:给出n个点的坐标(x,y),要求用线段将n个点连接起来,求最小的线段和. 最小生成树问题,用Kruskal算法进行求解,其中用到了并查集.将所有的点连接,构成一张图,对每一条边进行编号,两点 ...

  3. uva 10034 Freckles (kruskal||prim)

    题目上仅仅给的坐标,没有给出来边的长度,不管是prim算法还是kruskal算法我们都须要知道边的长度来操作. 这道题是浮点数,也没啥大的差别,处理一下就能够了. 有关这两个算法的介绍前面我已经写过了 ...

  4. uva 10032 Problem F: Tug of War

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. uva 10026 Problem C: Edit Step Ladders

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. (DP)uva 10036 Problem C: Divisibility

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88171#problem/F 代码: #include <cstdio> ...

  7. uva 10034

    计算所有点之间的权值   然后就是最小生成树 #include<cstring> #include<string> #include<cstdio> #includ ...

  8. uva 10036 Problem C: Divisibility

    题意:能否在一个整数序列的每相邻的两项之间添加一个加减号,使得最终结果能被一个给定整数K<=100整除. dp[i][j]表示第i个数取余k为j的布尔值. #include <cstdio ...

  9. uva 11400 Problem F Lighting System Design

    紫皮书题: 题意:让你设计照明系统,给你n种灯泡,每种灯泡有所需电压,电源,每个灯泡的费用,以及每个灯泡所需的数量.每种灯泡所需的电源都是不同的,其中电压大的灯泡可以替换电压小的灯泡,要求求出最小费用 ...

随机推荐

  1. 《SDN核心技术剖析和实战指南》2.3 OF-CONFIG配置管理协议小结

    OpenFlow协议定义了交换机和控制器交换数据的方式和规范,但并没有定义如何配置和管理必需的网络参数和网络资源,OF-CONFIG的提出就是为了对OpenFlow提供配置管理支持.如下图所示,OF- ...

  2. F - True Liars - poj1417(背包+并查集)

    题意:有这么一群人,一群好人,和一群坏人,好人永远会说实话,坏人永远说假话,现在给你一组对话和好人与坏人的数目P1, P2. 数据里面的no是A说B是坏人, yes代表A说B是好人,就是这样,问题能不 ...

  3. Mvc中DropDownList 和DropDownListFor的常用方法

    Mvc中DropDownList 和DropDownListFor的常用方法 一.非强类型: Controller:ViewData["AreId"] = from a in rp ...

  4. ngnix 一 入门指南

    翻译自:ngnix--Beginner Guide ##ngnix入门指南 本指南给出了nginx的基本介绍,并介绍了可以使用它的完成一些简单任务. 它假定nginx已经安装在读者的机器上. 如果不是 ...

  5. iOS8 Core Image In Swift:视频实时滤镜

    iOS8 Core Image In Swift:自己主动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift: ...

  6. [Redux] Store Methods: getState(), dispatch(), and subscribe()

    console.clear(); const counter = (state = 0, action) => { switch (action.type) { case 'INCREMENT' ...

  7. myqltransactionRollbackexception deadlock found when trying to get lock

    linux 下远程连接mysq 命令: mysql -h "1.0.0.1" -u username -p 1 获 取锁等待情况 可以通过检查 table_locks_waited ...

  8. linux 声音大小调整的命令

    alsamixer 输入上面的命令 回车即可看到图形界面,界面如下 ┌──────────────────────────── AlsaMixer v1.0.27.1 ──────────────── ...

  9. Cannot modify header information - headers already sent by

    有时候你在使用 header("Location: http://localhost/myblog/index.php/manager/listview");的时候会出现这个问题, ...

  10. spring源码分析

    编译问题 spring-4.0.5.release编译是用jdk8编译的,为啥可以运行在jdk7的环境? 源码分析 spring源码分析,由一个点各个击破,比如依赖注入,autowired. spri ...