zoukankan      html  css  js  c++  java
  • 最小生成树 ZOJ 1203 Swordfish

    1203    Swordfish

    A - Swordfish

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
     
    There exists a world within our world
    A world beneath what we call cyberspace.
    A world protected by firewalls,
    passwords and the most advanced
    security systems.
    In this world we hide
    our deepest secrets,
    our most incriminating information,
    and of course, a shole lot of money.
    This is the world of Swordfish.

      We all remember that in the movie Swordfish, Gabriel broke into the World Bank Investors Group in West Los Angeles, to rob $9.5 billion. And he needed Stanley, the best hacker in the world, to help him break into the password protecting the bank system. Stanley's lovely daughter Holly was seized by Gabriel, so he had to work for him. But at the last moment, Stanley made some little trick in his hacker mission: he injected a trojan horse in the bank system, so the money would jump from one account to another account every 60 seconds, and would continue jumping in the next 10 years. Only Stanley knew when and where to get the money. If Gabriel killed Stanley, he would never get a single dollar. Stanley wanted Gabriel to release all these hostages and he would help him to find the money back.
      You who has watched the movie know that Gabriel at last got the money by threatening to hang Ginger to death. Why not Gabriel go get the money himself? Because these money keep jumping, and these accounts are scattered in different cities. In order to gather up these money Gabriel would need to build money transfering tunnels to connect all these cities. Surely it will be really expensive to construct such a transfering tunnel, so Gabriel wants to find out the minimal total length of the tunnel required to connect all these cites. Now he asks you to write a computer program to find out the minimal length. Since Gabriel will get caught at the end of it anyway, so you can go ahead and write the program without feeling guilty about helping a criminal.

    Input:
    The input contains several test cases. Each test case begins with a line contains only one integer N (0 <= N <=100), which indicates the number of cities you have to connect. The next N lines each contains two real numbers X and Y(-10000 <= X,Y <= 10000), which are the citie's Cartesian coordinates (to make the problem simple, we can assume that we live in a flat world). The input is terminated by a case with N=0 and you must not print any output for this case.

    Output:
    You need to help Gabriel calculate the minimal length of tunnel needed to connect all these cites. You can saftly assume that such a tunnel can be built directly from one city to another. For each of the input cases, the output shall consist of two lines: the first line contains "Case #n:", where n is the case number (starting from 1); and the next line contains "The minimal distance is: d", where d is the minimal distance, rounded to 2 decimal places. Output a blank line between two test cases.

    Sample Input:
    5
    0 0
    0 1
    1 1
    1 0
    0.5 0.5
    0
    

    Sample Output:
    Case #1:
    The minimal distance is: 2.83
    

     解题思路:将点与点的距离作为开销保存起来就可以解决此题!

    解题代码:

    View Code
     1 // File Name: A - Swordfish
     2 // Author: sheng
     3 // Created Time: 2013年04月18日 星期四 19时03分44秒
     4 
     5 #include <iostream>
     6 #include <stdio.h>
     7 #include <string.h>
     8 #include <algorithm>
     9 #include <math.h>
    10 using namespace std;
    11 
    12 const int Maxn = 110;
    13 const int Maxp = 5000;
    14 struct node
    15 {
    16     double x, y;
    17 }coor[Maxn];
    18 
    19 struct Coor
    20 {
    21     int p1, p2;
    22     double w;
    23     bool operator < (const Coor po) const
    24     {
    25         return w < po.w;
    26     }
    27 }point[Maxp];
    28 
    29 int fa[Maxp];
    30 int rank[Maxp];
    31 double total;
    32 
    33 int Find (int x)
    34 {
    35     if ( x != fa[x])
    36         return fa[x] = Find(fa[x]);
    37     return x;
    38 }
    39 
    40 void Uin(int x, int y, double w)
    41 {
    42     if (x == y) return;
    43     if (rank[x] > rank[y])
    44     {
    45         fa[y] = x;
    46     }
    47     else
    48     {
    49         if (rank[x] == rank[y])
    50             rank[y] ++;
    51         fa[x] = y;
    52     }
    53     total += w;
    54 }
    55 
    56 int main()
    57 {
    58     int n;
    59     int count = 1;
    60     while (~scanf ("%d", &n) && n)
    61     {
    62         int cun = 0;
    63         for (int i = 0; i < n; i ++)
    64         {
    65             scanf ("%lf%lf", &coor[i].x, &coor[i].y);
    66             for (int j = 0; j < i; j ++)
    67             {
    68                 point[cun].p1 = j;
    69                 point[cun].p2 = i;
    70                 double temp_x = pow ( fabs (coor[i].x - coor[j].x), 2 );
    71                 double temp_y = pow ( fabs (coor[i].y - coor[j].y), 2 );
    72                 point[cun++].w = (double) sqrt ( temp_x + temp_y );
    73             }
    74         }
    75         if (count != 1) printf ("\n");
    76         printf ("Case #%d:\n", count ++);
    77         sort (point, point + cun);
    78         for (int i = 0; i < cun; i ++)
    79         {
    80             fa[i] = i;
    81             rank[i] = 0;
    82     //        cout << point[i].w << endl;;
    83         }
    84         total = 0;
    85         for (int i = 0; i < cun; i ++ )
    86         {
    87             int a = Find (point[i].p1);
    88             int b = Find (point[i].p2);
    89             if (a != b)
    90             {
    91                 Uin(a, b, point[i].w);
    92 
    93             }
    94         }
    95         printf ("The minimal distance is: %.2lf\n", total);
    96     }
    97 }
     
  • 相关阅读:
    关于hexo-abbrlink链接undefined
    如何修改layer-layui中的confirm
    cmder的segmentation fault错误修复
    论好的代码习惯的养成/做一个优雅的coder
    50行代码写的一个插件,破解一个H5小游戏
    慎用array_filter函数
    python:if 语句的使用方法
    python:for语句的使用方法
    关于python3 发送邮件
    zookpeer的安装与配置
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3034339.html
Copyright © 2011-2022 走看看