ZOJ Problem Set - 1203
来源:互联网 发布:美姿堂专业彩妆护淘宝 编辑:程序博客网 时间:2024/06/05 18:13
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
最裸的最小生成树了,kruskal算法,复杂度O(nlogn)。注意这题n=1时,应该特别输出0.00.
代码:
#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#define Maxn 110using namespace std;struct line{ int u,v; double w; bool operator<(const line &a)const{ return w<a.w; }}edge[Maxn*Maxn];double x[Maxn],y[Maxn];int fa[Maxn];double dis(int a,int b){ return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));}int init(int n){ for(int i=0;i<n;i++) fa[i]=i;}int findset(int x){ return x==fa[x]?x:(fa[x]=findset(fa[x]));}void unionset(int x,int y){ fa[x]=y;}double kruskal(int n,int tot){ int cnt=0; double res=0; for(int i=0;i<tot;i++){ int a=findset(edge[i].u),b=findset(edge[i].v); if(a!=b){ res+=edge[i].w; unionset(a,b); if(++cnt==n-1) return res; } }}int main(){ int n,cas=0; while(scanf("%d",&n),n){ if(++cas!=1) puts(""); for(int i=0;i<n;i++) scanf("%lf%lf",x+i,y+i); init(n); int tot=0; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) edge[tot++]=(line){i,j,dis(i,j)}; sort(edge,edge+tot); printf("Case #%d:\nThe minimal distance is: %.2f\n",cas,n==1?0:kruskal(n,tot)); }return 0;}
0 0
- ZOJ Problem Set - 1203
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 1049
- ZOJ Problem Set - 1051
- ZOJ Problem Set - 1067
- ZOJ Problem Set - 1115
- ZOJ Problem Set - 1151
- ZOJ Problem Set - 1205
- ZOJ Problem Set - 1025
- ZOJ Problem Set - 1029
- ZOJ Problem Set - 1076
- ZOJ Problem Set - 1117
- ZOJ Problem Set - 1041
- ZOJ Problem Set - 1004
- ZOJ Problem Set - 1005
- IOS学习笔记29—提示框第三方库之MBProgressHUD
- Host SMBus controller not enabled的解决方法
- oracle 存储过程中变量名和表的字段名同名时,优先将字段名当做表的字段名
- 九度oj-1008-最短路径问题
- Linux——虚拟内存
- ZOJ Problem Set - 1203
- hdu 3068最长回文子串
- Android应用程序的编译和打包
- C++11(4):语句
- C++ 中list 类模板的简易实现
- 第十一章 11.3.2节练习
- mybatis与hibernate区别
- libpcap的安装、工作原理及流程(引用综合)
- css 相对浏览器动态居中