Zoj1203swordfish (minimum spanning tree)

Source: Internet
Author: User

Swordfish Time limit: 2 Seconds Memory Limit: 65536 KB There exists a world within
A World beneath, what are 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 l OT of money.
This is the world of swordfish.

We All remember this in the movie swordfish, Gabriel broke to 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 broke 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 he hacker Mission:he injected a Trojan horse in the bank Syste M, so the "money would" jump from one account to another account every 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 have watched the movie know that Gabriel at last got the money by threatening to hang Ginger to death. Why isn't Gabriel go get the money himself? Because These money keep jumping, and these accounts is scattered in different cities. In order to gather-these money Gabriel would need to build money transfering tunnels to connect all these cities. Surely it'll 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 your to write a computer program to find out the the minimal length. Since Gabriel'll get caught at the end of it anyway, so can go ahead and write the program without feeling guilty AB Out 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 Y OU has to connect. The next N lines each contains the real numbers X and Y ( -10000 <= x, y <= 10000), which is the Citie ' s Cartesian COO Rdinates (To make the problem-simple, we can assume this we live in a flat world). The input is terminated by a case with n=0 and your must not the print any output for this case.

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

Sample Input:
50 00 11 11 00.5 0.50

Sample Output:
Case #1: The minimal distance is:2.83
is still the minimum spanning tree template problem, are good questions. The AC code is attached:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm>using namespace std;struct node{int start;int end;double Dis;} T[10010];int CMP (node A,node b) {return a.dis<b.dis;} int per[5500];int Find (int x) {int r=x;while (R!=per[r]) R=per[r];return R; int join (int x,int y) {int fx=find (x), int fy=find (y), if (fx!=fy) {Per[fx]=fy;return 1;} return 0;} int main () {int flag=1;int m,n,i,j;double x[110],y[110];while (scanf ("%d", &n), N) {for (i=0;i<5500;i++) per[i]=i; for (i=0;i<n;i++) scanf ("%lf%lf", &x[i],&y[i]), int k=0;for (i=0;i<n;i++) for (j=i+1;j<n;j++) {t[k]. Start=i;t[k].end =j;t[k].dis =sqrt ((X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j]); k++;} Sort (t,t+k,cmp);d ouble sum=0;for (i=0;i<k;i++) if (Join (t[i].start,t[i].end)) sum=sum+t[i].dis;if (flag!=1) printf ("\ n");p rintf ("#%d:\n", "flag");p rintf ("The Minimal distance is:");p rintf ("%.2lf\n", sum); flag++;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Zoj1203swordfish (minimum spanning tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.