8.3 E

来源:互联网 发布:家具有味怎么办知乎 编辑:程序博客网 时间:2024/06/08 01:29

                                      E - 这是水题5



There exists a world within our world 
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. 

  We all remember that in the movie Swordfish, Gabriel broke into 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 break 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 his hacker mission: he injected a trojan horse in the bank system, so the money would jump from one account to another account every 60 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 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<algorithm>#include<string.h>#include<math.h>using namespace std;struct edge{int u,v;double w;};struct edge e[50000];int n,m;int f[50000];double sum;double cmp(edge a,edge b)//sort排序{return a.w < b.w ;}int getf(int v)//并查集搜索函数{if(f[v]==v){return v;}else{f[v]=getf(f[v]);return f[v];}}int merge(int v,int u)//合并并查集函数{int t1,t2;t1=getf(v);t2=getf(u);if(t1!=t2){f[t2]=t1;return 1;}return 0;}int main(){int i,j,t,c,z=1;double x[2000],y[2000];while(scanf("%d",&n)!=EOF){if(n==0)break;sum=0;c=0;t=0;memset(f,0,sizeof(f));for(i=1;i<=n;i++){scanf("%lf%lf",&x[i],&y[i]);//读取数据}for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){t++;e[t].u=i;e[t].v=j;e[t].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));//勾股定理计算城市之间的距离}}    sort(e+1,e+1+t,cmp);for(i=1;i<=t;i++){    f[i]=i;}for(i=1;i<=t;i++){if(merge(e[i].u,e[i].v )){c++;sum+=e[i].w;}if(c == n-1)break;}if(z!=1)//用于标记第几次输出和控制输出换行第一个输出不加换行,后面的输出前加换行printf("\n");printf("Case #%d:\n",z);printf("The minimal distance is: %.2lf\n",sum);z++;}return 0;}




 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果描述文件没有了怎么办 苹果6s发热严重怎么办 苹果6s发烫严重怎么办 手机型号不适配全军出击怎么办? 苹果5版本过低怎么办 在泰国买到假货怎么办 在泰国旅游遇到危险该怎么办? 拾到小米手环怎么办 苹果充电打游戏触点怎么办 衣服上金属锈了怎么办 光之子门锁住了怎么办 挖到金矿石了该怎么办 邻居是精神病扔石头怎么办 汽车保养手册丢了怎么办 五号电池没电了怎么办 电脑打印时显示脱机怎么办 博新计划两年后怎么办 门帘粘扣老是掉怎么办 魔术粘贴带双面背胶不粘了怎么办 斜挎包包磨衣服怎么办 电脑ip被别人知道了怎么办 西乐葆不止痛了怎么办 家里鸽子多捣蛋的不行怎么办 联璧倒闭了理财钱怎么办 众筹平台雷了怎么办 App理财无法提现怎么办 喝咖啡喝的心慌怎么办 睾丸被用力捏破流血怎么办 足球大师球队声望过高怎么办 fm17买了好多球员怎么办 fm2018电脑适应了战术怎么办 家纺摆放跑色怎么办 买的t恤太小了怎么办? 汽车全包围脚垫翘边怎么办 针织衣服线跑了怎么办 老板跟老板娘吵架我应该怎么办 偷看老板娘洗澡被发现怎么办? 幽浮2人挂了怎么办 蜻蜓fm业务扣费怎么办 退出键退不到主页面怎么办 3d纯英文版怎么办