zoukankan      html  css  js  c++  java
  • HDU

    先上题目:

    World Islands

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 888    Accepted Submission(s): 345


    Problem Description
    Dubai is a haven for the rich. The government of Dubai finds a good way to make money. They built a lot of artificial islands on the sea and sell them. These islands are shaped into the continents of the world, so they are called “world islands”. All islands are booked out now. The billionaires who buy these islands wants to make friends with each other, so they want these islands all be connected by bridges. Bill Gates also has booked an island, but he is the only one who doesn’t want his island to be connected with other islands, because he prefer to travel on the sea on his old landing craft which is used in the Invasion of Normandy in World War II. Fortunately, Bill doesn’t care about which island is saved for him, so Dubai government can still find the best way to build the bridges. The best way means that the total length of the bridges is minimum. In a word, if there are n islands, what they should do is to build n–2 bridges connecting n-1 islands, and give the rest island to Bill Gates. They can give any island to Bill Gates. Now they pay you good money to help them to find out the best way to build the bridges. 
    Please note;
    1.An island can be considered as a point.
    2.A bridge can be considered as a line segment connecting two islands.
    3.A bridge connects with other bridges only at the islands.
     
    Input
    The first line is an integer indicating the number of test cases.
    For each test case, the first line is an integer n representing the number of islands.(0<n<50)
    Then n lines follow. Each line contains two integers x and y( -20 <= x, y <= 20 ) , indicating the coordinate of an island.
     
    Output
    For each test case, output the minimum total length of the bridges in a line. The results should be rounded to 2 digits after decimal point, and you must keep 2 digits after the decimal point.
     
    Sample Input
    2
    5
    0 0
    1 0
    18 0
    0 1
    1 1
    3
    0 0
    1 0
    0 1
     
    Sample Output
    3.00
    1.00
     
    Source
     
      题意:给你n个点的坐标,问你选出n-1个点使它们连通,并保证边的总长度最小。
      做法:做小生成树+枚举。
      直接枚举不选那个点,然后直接跑最小生成树就可以了。
     
    上代码:
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <algorithm>
     5 #define MAX 100
     6 using namespace std;
     7 
     8 const double eps=1e-3;
     9 
    10 int p[MAX];
    11 int n;
    12 int dcmp(double x){
    13     if(fabs(x)<eps) return 0;
    14     return x>0 ? 1 : -1;
    15 }
    16 
    17 typedef struct Point{
    18     double x,y;
    19     Point(double x=0,double y=0):x(x),y(y){}
    20 }Point;
    21 
    22 Point s[MAX];
    23 int id[MAX];
    24 
    25 typedef struct edge{
    26     int a,b;
    27     double l;
    28 
    29     bool operator < (const edge& o)const{
    30         return dcmp(l-o.l)<0;
    31     }
    32 
    33 }edge;
    34 
    35 edge e[MAX*MAX];
    36 int tot;
    37 
    38 double dist(Point a,Point b){
    39     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    40 }
    41 
    42 int findset(int x){ return p[x]==x ? p[x] : p[x]=findset(p[x]);}
    43 
    44 
    45 
    46 int main()
    47 {
    48     int t,co;
    49     double ans,minn;
    50     //freopen("data.txt","r",stdin);
    51     scanf("%d",&t);
    52     while(t--){
    53         scanf("%d",&n);
    54         for(int i=0;i<n;i++){
    55             scanf("%lf %lf",&s[i].x,&s[i].y);
    56         }
    57         tot=0;
    58         for(int i=0;i<n;i++){
    59             for(int j=i+1;j<n;j++){
    60                 double l=dist(s[i],s[j]);
    61                 e[tot].a=i; e[tot].b=j; e[tot].l=l;
    62                 tot++;
    63             }
    64         }
    65         sort(e,e+tot);
    66         minn=1e12;
    67         for(int i=0;i<n;i++){
    68             ans=0;
    69             for(int j=0;j<n;j++){
    70                 p[j]=j;
    71             }
    72             co=0;
    73             for(int j=0;j<tot && co!=n-2;j++){
    74                 if(e[j].a==i || e[j].b==i) continue;
    75                 int a=findset(e[j].a),b=findset(e[j].b);
    76                 if(p[a]!=p[b]){
    77                     p[b]=p[a];
    78                     ans+=e[j].l;
    79                     co++;
    80                 }
    81             }
    82             minn=min(ans,minn);
    83         }
    84         if(n==1) minn=0;
    85         printf("%.2lf
    ",minn);
    86     }
    87     return 0;
    88 }
    /*3405*/
     
  • 相关阅读:
    CSS盒子模式(DIV布局快速入门)
    CSS中的滑动门技术
    由浅入深漫谈margin属性
    zz Apache 虚拟主机 VirtualHost 配置
    动态生成编译运行java类
    ubuntu 手动设置DNS服务器,重启后不能上网
    ubuntu下部署发布环境
    zz [Java]读取文件方法大全
    Ubuntu apache2 主机配置文件
    JAVA的CALLBACK
  • 原文地址:https://www.cnblogs.com/sineatos/p/3946727.html
Copyright © 2011-2022 走看看