A - Swordfish
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.
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
解题思路:将点与点的距离作为开销保存起来就可以解决此题!
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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 }