ZOJ 1203 Swordfish【最小生成树】
来源:互联网 发布:猫眼网络大电影数据 编辑:程序博客网 时间:2024/06/07 10:40
ZOJ Problem Set - 1203
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
给出若干个点的坐标,求出让他们连接起来需要的最短的长度,最小生成树,克鲁斯卡尔算法,比较简单,水过.....
注意每两个数据之间的空行.....
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;int per[105],n,cnt,kase,t=0;double a[105],b[105];struct lu{int a,b;double len;}x[5005];double dis(int i,int j){return sqrt((a[i]-a[j])*(a[i]-a[j])*1.0+(b[i]-b[j])*(b[i]-b[j]));}int init(){for(int i=1;i<=n;++i){per[i]=i;}}bool cmp(lu a,lu b){return a.len<b.len;}int find(int x){int r=x;while(r!=per[r]){r=per[r];}int i=x,j;while(i!=r){j=per[i];per[i]=r;i=j;}return r;}int join(int x,int y){int fx=find(x),fy=find(y);if(fx!=fy){per[fy]=fx;return 1;}return 0;}void kruskal(){cnt=0;double sum=0;for(int i=0;cnt<n-1;++i){if(join(x[i].a,x[i].b)){sum+=x[i].len;++cnt;}}printf("Case #%d:\nThe minimal distance is: %.2lf\n",++t,sum);}int main(){int i,j;while(scanf("%d",&n),n){init();for(i=0;i<n;++i){scanf("%lf%lf",a+i,b+i);}int c=0;for(i=0;i<n-1;++i){for(j=i+1;j<n;++j){x[c].a=i+1;x[c].b=j+1;x[c].len=dis(i,j);++c;}}sort(x,x+c,cmp);if(t){printf("\n");}kruskal();}return 0;}
0 0
- zoj 1203 swordfish 最小生成树
- ZOJ 1203 Swordfish(最小生成树 kruskal)
- ZOJ 1203 Swordfish【最小生成树】
- ZOJ 1203 Swordfish (最小生成树 prim算法)
- ZOJ - 1203 Swordfish (最小生成树 Prim)
- zoj 1203 Swordfish(最小生成树))
- zoj 1203 Swordfish 【最小生成树 kruskal && prim】
- ZOJ 1203 Swordfish 最小生成树 Kruscal && Prim
- ZOJ - 1203 Swordfish (非负权值的最小生成树/最短路 - Kruskal算法)
- 最小生成树之Swordfish
- 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
- find the nth digit
- [图论]连接岛屿问题(MST.Kruskal算法 并查集)
- UILabel用法大全
- Android-项目开发基础—JNI与底层调用(十三)
- UITabBarController标签导航控制器的使用
- ZOJ 1203 Swordfish【最小生成树】
- 【大数据工程师之路】Hadoop——MapReduce概述
- Max Sum(HDU 1003)
- layoutSubviews何时调用的问题
- Android使用ImageLoader异步加载网络图片(一)读取单张图片
- STM32——GPIO输入模式下上拉和下拉的设置
- Ubuntu下安装Eclipse
- HashMap详解
- final方法和final类,内部类