ZOJ 1203 Swordfish
来源:互联网 发布:mac如何连接远程服务 编辑:程序博客网 时间:2024/05/27 16:41
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:
50 00 11 11 00.5 0.50
Sample Output:
Case #1:The minimal distance is: 2.83
Source: Zhejiang University Local Contest 2002, Preliminary
简单最小生成树
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;struct node{int from,to;double vis;}t[11000];bool cmp(node a,node b){return a.vis<b.vis;}int pre[11000];int find(int x){if(x==pre[x])return x;return pre[x]=find(pre[x]);}int merge(int x,int y){int fx=find(x);int fy=find(y);if(fx!=fy){pre[fx]=fy;return 1;}return 0;}double dis(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}int main(){int n,i,j;double x[11000],y[11000];int flag=1;while(scanf("%d",&n),n){for(i=0;i<=n;i++)pre[i]=i;for(i=1;i<=n;i++)scanf("%lf%lf",&x[i],&y[i]);int k=0;for(i=1;i<=n;i++)for(j=1;j<=n;j++){t[k].from=i;t[k].to=j;t[k++].vis=dis(x[i],y[i],x[j],y[j]);}sort(t,t+k,cmp);double sum=0.0;for(i=0;i<k;i++){if(merge(t[i].from,t[i].to))sum+=t[i].vis;}if(flag!=1)printf("\n");printf("Case #%d:\n",flag);printf("The minimal distance is: %.2lf\n",sum);flag++;}return 0;}
0 0
- ZOJ 1203 Swordfish ( prim )
- zoj 1203 Swordfish(Prim!)
- zoj 1203 Swordfish【prim】
- Prim zoj 1203 Swordfish
- zoj - 1203 - Swordfish
- zoj 1203 Swordfish
- zoj 1203 Swordfish MST
- ZOJ 1203 - Swordfish
- ZOJ-1203 Swordfish
- ZOJ-1203-Swordfish
- ZOJ:1203 Swordfish
- zoj 1203 Swordfish
- ZOJ 1203 Swordfish MST
- ZOJ 1203 Swordfish
- zoj 1203 Swordfish
- ZOJ-1203 Swordfish
- zoj 1203 Swordfish
- zoj 1203 Swordfish
- android学习之旅--BaseAdapter细节
- ssh免密码登陆简单原理
- codeforces--652B z-sort(sorting)
- 关闭sublime更新提醒
- Java中的多态性
- ZOJ 1203 Swordfish
- ubuntu12.04设置屏幕开机亮度
- 流量劫持与防范
- sublime 更改字体大小
- redis主从复制
- 联合体(union)的使用方法及其本质
- QGraphicsView框架容易忽视的坐标问题-“画布”QgraphicsScene到底放在了哪儿?
- 解决问题, 才是首要目的
- $.fn.extend 与$.extend