Problem Description

相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。

Input

输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。

Output

每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.

Sample Input

  1.  

2 2 10 10 20 20 3 1 1 2 2 1000 1000

Sample Output

  1.  

1414.2 oh!

最小生成树板子题吧,判断一下所有在条件范围内的边,然后跑一遍Kruskal 如果最后边数不是n-1就输出oh!

有空再补一下Prim的做法~~

  1. #include<iostream>
  2. #include<cstdio> //EOF,NULL
  3. #include<cstring> //memset
  4. #include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
  5. #include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
  6. #include<algorithm> //fill,reverse,next_permutation,__gcd,
  7. #include<string>
  8. #include<vector>
  9. #include<queue>
  10. #include<stack>
  11. #include<utility>
  12. #include<iterator>
  13. #include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
  14. #include<functional>
  15. #include<map>
  16. #include<set>
  17. #include<limits.h> //INT_MAX
  18. #include<bitset> // bitset<?> n
  19. using namespace std;
  20.  
  21. #define rep(i,a,n) for(int i=a;i<n;i++)
  22. #define per(i,a,n) for(int i=n-1;i>=a;i--)
  23. #define fori(x) for(int i=0;i<x;i++)
  24. #define forj(x) for(int j=0;j<x;j++)
  25. #define memset(x,y) memset(x,y,sizeof(x))
  26. #define memcpy(x,y) memcpy(x,y,sizeof(y))
  27. #define all(x) x.begin(),x.end()
  28. #define readc(x) scanf("%c",&x)
  29. #define read(x) scanf("%d",&x)
  30. #define read2(x,y) scanf("%d%d",&x,&y)
  31. #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  32. #define print(x) printf("%d\n",x)
  33. #define lowbit(x) x&-x
  34. #define lson(x) x<<1
  35. #define rson(x) x<<1|1
  36. #define pb push_back
  37. #define mp make_pair
  38. typedef pair<int,int> P;
  39. typedef long long LL;
  40. typedef long long ll;
  41. const double eps=1e-;
  42. const double PI = acos(1.0);
  43. const int INF = 0x3f3f3f3f;
  44. const int inf = 0x3f3f3f3f;
  45. const int mod = 1e9+;
  46. const int MAXN = 1e6+;
  47. const int maxm = ;
  48. const int maxn = +;
  49.  
  50. int T;
  51. int n;
  52. double x[maxn],y[maxn];
  53. int cnt,tot;
  54. double ans ;
  55. int pre[maxn];
  56. struct rood{
  57. int st,ed;
  58. double w;
  59. bool operator < (rood b) const{
  60. return w < b.w ;
  61. }
  62. }rod[maxn];
  63. struct node{
  64. double x,y;
  65. }pot[maxn];
  66.  
  67. int find(int x) {return x == pre[x] ? x : pre[x] = find(pre[x]);}
  68. bool join(int x,int y){
  69. if(find(x) != find(y)) {
  70. pre[find(y)] = find(x);
  71. return true;
  72. }
  73. return false;
  74. }
  75. double cmp(node a,node b){
  76. double ans = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
  77. return sqrt(ans);
  78. }
  79. void kruskal(){
  80. for(int i = ;i < cnt ; i++){
  81. int mp1 = find(rod[i].st);
  82. int mp2 = find(rod[i].ed);
  83. if(join(mp1,mp2)) {
  84. ans += rod[i].w;
  85. tot++;
  86. }
  87. }
  88. }
  89.  
  90. int main(){
  91. read(T);
  92. while(T--){
  93. read(n);
  94. for(int i = ; i < n; i++){
  95. scanf("%lf %lf",&pot[i].x,&pot[i].y);
  96. }
  97. memset(rod,);
  98. ans = ;
  99. cnt = tot = ;
  100. for(int i = ; i < maxn ;i ++)
  101. pre[i] = i;
  102. for(int i = ; i < n - ; i++){
  103. for(int j = i + ; j < n ; j++){
  104. double dist = cmp(pot[i],pot[j]);
  105. if( dist >= && dist <= ) {
  106. rod[cnt].st = i;
  107. rod[cnt].ed = j;
  108. rod[cnt++].w = dist;
  109. }
  110. }
  111. }
  112. sort(rod,rod + cnt);
  113. kruskal();
  114. if(tot != n-)
  115. printf("oh!\n");
  116. else
  117. printf("%.1lf\n",ans * );
  118. }
  119. }

HDU - 1875 畅通工程再续【最小生成树】的更多相关文章

  1. HDU 1875 畅通工程再续 (最小生成树)

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. (step6.1.3)hdu 1875(畅通工程再续——最小生成树)

    题目大意:本题是中文题,可以直接在OJ上看 解题思路:最小生成树 1)本题的关键在于把二维的点转化成一维的点 for (i = 0; i < n; ++i) { scanf("%d%d ...

  3. hdu 1875 畅通工程再续(prim方法求得最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875 /************************************************* ...

  4. HDU 1875 畅通工程再续 (prim最小生成树)

    B - 畅通工程再续 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...

  5. HDU 1875 畅通工程再续 (最小生成树)

    畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...

  6. HDU 1875 畅通工程再续(kruskal)

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. HDU 1875 畅通工程再续 (Prim)

    题目链接:HDU 1875 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现 ...

  8. HDU - 1875 畅通工程再续(最小生成树)

    d.c个小岛,通过建立桥,使其全部可达.求所有的桥的最小长度和. s.最小生成树,数据改成double就行了 c.Prim算法:cost[a][b]和cost[b][a]都得赋值. /* Prim算法 ...

  9. hdu 1875 畅通工程再续(最小生成树,基础)

    题目 让人郁闷的题目,wa到死了,必须要把判断10.0和1000.0的条件放到prim函数外面去.如代码所放.... 正确的(放在prim外): //2个小岛之间的距离不能小于10米,也不能大于100 ...

随机推荐

  1. CSS3实现GIF动画

    来自 dribbble 某位大师的作品,GIF图中一个小女孩抱着一只猫在跑步,非常可爱,动作轻巧,过渡自然.DEMO下载 回到项目需求,要实现类似上图卡通人物跑步动画,分析结果如下: 1.跑步动画可以 ...

  2. C语言---变量与函数

    一个C程序是由一个或多个程序模块组成的,每一个程序模块作为一个源程序文件,一个源程序文件是一个编译单元. 源程序文件分为库函数和用户自己定义的函数,以及有参函数.无参函数. 函数调用的过程: 1) 定 ...

  3. CSS常用样式属性

    1.CSS字体和文本相关属性 属性 font-family 规定文本的字体系列,比如:“serif” ''sans-serif" font-size 规定文本的字体尺寸 font-style ...

  4. html5-字体css

    #div1{font-size: 50px;}#div2{font-size: 50%;}#div3{font-size: 300%}#div4{font-size: 3em;}#div5{font- ...

  5. map 的用法

    #include<iostream> #include<map> #include<string> #define s second #define f first ...

  6. Python数据可视化-seaborn

    详细介绍可以看seaborn官方API和example galler. 1  set_style( )  set( ) set_style( )是用来设置主题的,Seaborn有五个预设好的主题: d ...

  7. 【2017-03-20】HTML基础知识,标记,表格,表格嵌套及布局,超链接

    一.HTML  网站(站点),网页基础知识 HTML是一门编程语言的名字:超文本标记语言 可以理解为:超越了文本的范畴,可以有图片.视频.音频.动画特效等其他内容,用标记的方法进行编程的计算机语言 基 ...

  8. python 查询文本文件的层次

    I/O系统有一系列的层次构建而成 下面是操作一个文本文件的例子来查看这种层次 >>> f = open('sample.txt','w') >>> f <_i ...

  9. redux 数据规律

    counter increase info  todos 为 reducers 文件名 export default combineReducers({ todos, visibilityFilter ...

  10. Vue.js是什么,vue介绍

    Vue.js是什么,vue介绍 Vue.js 是什么Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用. ...