[2016-04-13] [HDU] [1875] [Smooth works re-continued]

Source: Internet
Author: User

    • Time: 2016-04-13-23:58:24
    • Title Number: [2016-04-13][hdu][1875][smooth works re-continued]
    • The smallest spanning tree in the specified range is the right edge
    • Analysis: Kruskal The merger of the time to judge, and finally determine whether the combined edge number is n-1
  
 
  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cmath>
  4. using namespace std;
  5. const int maxn = 100 + 10;
  6. struct Point{
  7. int x,y;
  8. Point(int a=0,int b=0):x(a),y(b){}
  9. }p[maxn];
  10. struct Edge{
  11. int v C ;
  12. edge ( int a = 0 int b = 0 int cc = 0 ): u ( a v ( b c ( cc ) {}
  13. bool operator < (const Edge & a)const{
  14. return c < a.c;
  15. }
  16. }e[maxn * maxn];
  17. int fa[maxn];
  18. int fnd(int x){
  19. return x == fa[x]?x:fa[x] = fnd(fa[x]);
  20. }
  21. void ini(int n){
  22. for(int i = 0 ; i <= n ; ++i){
  23. fa[i] = i;
  24. }
  25. }
  26. int dis(int a,int b){
  27. return (P[a].x-P[b].x)*(P[a].x-P[b].x) + (P[a].y-P[b].y) * (P[a].y-P[b].y);
  28. }
  29. int main(){
  30. int t;
  31. scanf("%d",&t);
  32. while(t--){
  33. int n;
  34. scanf("%d",&n);
  35. ini(n);
  36. for(int i = 0 ; i < n ; ++i){
  37. scanf("%d%d",&p[i].x,&p[i].y);
  38. }
  39. int cnt = 0;
  40. for ( int i = 0 I < n ++ i ) {
  41. for ( int J = 0 ; J < n ++ j ) {
  42. e[cnt++] = Edge(i,j,dis(i,j));
  43. }
  44. }
  45. sort(e,e+cnt);
  46. int k = 0;double ans = 0;
  47. for(int i = 0 ; i < cnt ; ++i){
  48. int f1 = fnd(e[i].u);
  49. int F2 = fnd ( E [ i . v
  50. if ( f1 != F2 && e [ i c >= 100 && e [ i . c <= 1000 * 1000 Span class= "pun") {
  51. fa[f1] = f2;
  52. ans += sqrt(double( e[i].c));
  53. ++k;
  54. }
  55. if(k == n - 1) break;
  56. }
  57. if(k == n - 1)
  58. printf("%.1lf\n",ans * 100);
  59. else puts("oh!");
  60. }
  61. return 0;
  62. }


From for notes (Wiz)

[2016-04-13] [HDU] [1875] [Smooth works re-continued]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.