zoukankan      html  css  js  c++  java
  • HDU 4463 Outlets(最小生成树给坐标)

    Problem Description
    In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
    So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
     
    Input
    There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.
     
    Output
    For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.
     
    Sample Input
    4
    2 3
    0 0
    1 0
    0 -1
    1 -1
    0
     
    Sample Output
    3.41
     
    题意:要求一棵最小生成树,要求nike店与apple店直接相连,Input给的是各点的坐标。用坐标转化出距离,即转化为最小生成树裸题。
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<vector>
     5 #include<cstring>
     6 #include<string>
     7 #include<algorithm>
     8 #include<map>
     9 #include<cmath>
    10 #include<math.h>
    11 using namespace std;
    12 
    13 int fa[305];
    14 
    15 struct node
    16 {
    17     int u,v;
    18     double c;
    19 }a[3005];
    20 
    21 struct point
    22 {
    23     double x,y;
    24 }Point[55];
    25 
    26 bool cmp(node b,node d)
    27 {
    28     return b.c<d.c;
    29 }
    30 
    31 int find(int t)
    32 {
    33     return fa[t]==t ? t:fa[t]=find(fa[t]);
    34 }
    35 
    36 double dis(point b,point d)
    37 {
    38     double res;
    39     res=sqrt((b.x-d.x)*(b.x-d.x)+(b.y-d.y)*(b.y-d.y));
    40     return res;
    41 }
    42 
    43 int main()
    44 {
    45     
    46     int n,m;
    47     double ans;
    48     int p,q;
    49     while(~scanf("%d",&n)&&n)
    50     {
    51         ans=0.0;
    52         m=0;
    53         scanf("%d%d",&p,&q);
    54         for(int i=1;i<=n;i++)
    55         {
    56             scanf("%lf%lf",&Point[i].x,&Point[i].y);
    57         }
    58         for(int i=1;i<=n;i++)
    59             for(int j=i+1;j<=n;j++)
    60             {
    61                 a[m].u=i;
    62                 a[m].v=j;
    63                 a[m++].c=dis(Point[i],Point[j]);
    64             }
    65         sort(a,a+m,cmp);
    66         for(int i=1;i<=n;i++)
    67             fa[i]=i;
    68         fa[p]=q;
    69         ans+=dis(Point[p],Point[q]);
    70         for(int i=0;i<m;i++)
    71         {
    72             int p1=find(a[i].u),q1=find(a[i].v);
    73             if(p1!=q1)
    74             {
    75                 fa[p1]=q1;
    76                 ans+=a[i].c;
    77             }
    78         }
    79         printf("%.2lf
    ",ans);
    80 
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    SQL Cookbook:二、查询结果排序(4)对字母数字混合的数据排序
    (转).net框架读书笔记引用参数(ref/out)
    (转).net面试问答(大汇总)
    SQL Cookbook:一、检索记录(9)限制返回的行数
    C# 3.0 Cookbook:一、字符与字符串处理(5):把一个字符串与另一个字符串的头部或尾部作比较
    (转)sql海量数据优化
    (转)C# 中的委托和事件(二)
    (转)我看微软.NET各技术应用前景
    在sql server数据库中快速删除记录,清空表
    SQL Cookbook:二、查询结果排序(3)按子串排序
  • 原文地址:https://www.cnblogs.com/Annetree/p/7198830.html
Copyright © 2011-2022 走看看