zoukankan      html  css  js  c++  java
  • Outlets(最小生成树)

    Outlets

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


    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


    42 30 01 00 -1 1 -10




    Sample Output


    3.41




    Source

     2012 Asia Hangzhou Regional Contest 



    Statistic |Submit |Discuss |Note

    最小生成树简单应用

     1 #include <iostream>
     2 #include <fstream>
     3 #include <string>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <cstring>
     7 #include <algorithm>
     8 #include <map>
     9 #include <vector>
    10 #include <queue>
    11 #include <stack>
    12 #define LL long long
    13 #define MAXI 2147483647
    14 #define MAXL 9223372036854775807
    15 #define eps (1e-8)
    16 #define dg(i) cout << "*" << i << endl;
    17       
    18 using namespace std;
    19       
    20 struct Position
    21 {
    22     int x, y;
    23 }pos[51];
    24       
    25 struct Edge
    26 {
    27     int u, v;
    28     double dis;
    29 }e[49 * 25 + 1];
    30       
    31 int set[51];
    32       
    33 double Sqrt(const Position a, const Position b)
    34 {
    35     return sqrt(1.0*(a.x - b.x)*(a.x - b.x) + 1.0*(a.y - b.y)*(a.y - b.y));
    36 }
    37       
    38 int Find(int x)
    39 {
    40     if(x != set[x]) set[x] = Find(set[x]);
    41     return set[x];
    42 }
    43       
    44 bool operator < (const Edge& x, const Edge& y)
    45 {
    46     return x.dis < y.dis;
    47 }
    48       
    49 int main()
    50 {
    51     int N, p, q;
    52     double minDis;
    53     int i, j, k, cnt;
    54     while(scanf("%d", &N) && N)
    55     {
    56         scanf("%d %d", &p, &q);
    57         for(i = 0; i < N; ++i)
    58         {
    59             set[i] = i;
    60             scanf("%d %d", &pos[i].x, &pos[i].y);
    61         }
    62         minDis = Sqrt(pos[p-1], pos[q-1]);
    63         set[p-1] = q - 1;
    64         if(p > q) swap(p, q);
    65         for(k = 0, i = 0; i < N - 1; i++)
    66         {
    67             for(j = i + 1; j < N; j++, k++)
    68             {
    69                 e[k].u = i;
    70                 e[k].v = j;
    71                 if(i == p - 1 && j == q - 1) e[k].dis = 0.0;
    72                 else e[k].dis = Sqrt(pos[i], pos[j]);
    73                 //cout << i << " " << j << " " << e[k].dis << endl;
    74             }
    75         }
    76         sort(e, e + k);
    77         for(cnt = 1, i = 1; i < k; i++)
    78         {
    79             int su = Find(e[i].u);
    80             int sv = Find(e[i].v);
    81             if(su != sv)
    82             {
    83                 if(su > sv) set[su] = sv;
    84                 else if(su < sv) set[sv] = su;
    85                 minDis += e[i].dis;
    86                 if(++cnt == N - 1) break;
    87             }
    88         }
    89         printf("%.2lf\n", minDis);
    90     }
    91     return 0;
    92 }


  • 相关阅读:
    Linq To Sql 练习
    伪静态在webconfig中配置
    log4net.dll配置以及在项目中应用
    C#Windows服务安装
    .net平台推送ios消息
    asp.net下js调用session
    MAC地址泛洪攻击测试
    wifipineapple使用教程
    python程序的调试方法
    python import的用法
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910433.html
Copyright © 2011-2022 走看看