UVa 10397 - Connect the Campus (最小生成树)

2023-12-10 04:48

本文主要是介绍UVa 10397 - Connect the Campus (最小生成树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1338


题目:

Problem E
Connect the Campus
Input:
 standard input
Output: standard output
Time Limit: 2 seconds

Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is connected to every other building (directly or indirectly) through the campus network of communication cables.

We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings).

You have been given a campus map which shows the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of new cable that you use.

Fig: University of Waterloo Campus

 

Input

The input file describes several test case.  The description of each test case is given below:

The first line of each test case contains the number of buildings N (1<=N<=750). The buildings are labeled from 1 to N. The next Nlines give the x and y coordinates of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 <= M <= 1000) followed byM lines describing the existing cables. Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.

Output

For each set of input, output in a single line the total length of the new cables that you plan to use, rounded to two decimal places.

Sample Input

4
103 104
104 100
104 103
100 100
1
4 2

4
103 104

104 100

104 103

100 100

1

4 2

 

Sample Output
4.41
4.41


分析与总结:

最小生成树, 但是有点变化,有些边是已经建好的,那么把这些建好的边权值赋值为0,然后直接模板



代码:

1.Prim

#include<cstdio>
#include<cstring>
#include<cmath>
#define N 760
double w[N][N],x[N],y[N],key[N];
int pre[N], hash[N], n, m;inline double getDist(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double Prim(){memset(hash, 0, sizeof(hash));hash[1] = 1;for(int i=1; i<=n; ++i){key[i] = w[1][i]; pre[i]=1;}double sum=0;for(int i=1; i<n; ++i){int u=-1;for(int j=1; j<=n; ++j)if(!hash[j]){if(u==-1||key[j]<key[u])u=j;}sum += w[pre[u]][u];hash[u] = 1;for(int j=1; j<=n; ++j)if(!hash[j]){if(key[j]>w[u][j]){key[j] = w[u][j];pre[j] = u;}}}return sum;
}int main(){int u,v;while(~scanf("%d",&n)){memset(w, 0, sizeof(w));for(int i=1; i<=n; ++i)scanf("%lf%lf",&x[i],&y[i]);for(int i=1; i<=n; ++i)for(int j=1; j<=n; ++j)if(i!=j)w[i][j] = getDist(x[i],y[i],x[j],y[j]);scanf("%d",&m);for(int i=0; i<m; ++i){scanf("%d%d",&u,&v);w[u][v]=w[v][u]=0;}printf("%.2f\n", Prim());}return 0;
}


2.Kruskal

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 760
using namespace std;
double w[N][N],x[N],y[N];
int f[N*N], rank[N*N], n, m;struct Edge{int u,v;double val;friend bool operator<(const Edge&a,const Edge&b){return a.val < b.val;}
}arr[N*N];inline double getDist(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}void init(){for(int i=0; i<=n*n; ++i)f[i]=i, rank[i]=0;
}
int find(int x){int i, j=x;while(j!=f[j]) j=f[j];while(x!=j){i=f[x]; f[x]=j; x=i;}return j;
}
bool Union(int x,int y){int a=find(x),b=find(y);if(a==b)return false;if(rank[a]>rank[b])f[b]=a;else{if(rank[a]==rank[b])++rank[b];f[a]=b;}return true;
}int main(){int u,v;while(~scanf("%d",&n)){memset(w, 0, sizeof(w));for(int i=1; i<=n; ++i)scanf("%lf%lf",&x[i],&y[i]);for(int i=1; i<=n; ++i)for(int j=1; j<=n; ++j)if(i!=j)w[i][j] = getDist(x[i],y[i],x[j],y[j]);scanf("%d",&m);for(int i=0; i<m; ++i){scanf("%d%d",&u,&v);w[u][v]=w[v][u]=0;}int pos=0;for(int i=1; i<=n; ++i)for(int j=i+1; j<=n; ++j){arr[pos].u=i,arr[pos].v=j;arr[pos++].val = w[i][j];}init();sort(arr,arr+pos);double ans=0;for(int i=0; i<pos; ++i){if(Union(arr[i].u,arr[i].v))ans += arr[i].val;}printf("%.2f\n", ans);}return 0;
}


——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)





这篇关于UVa 10397 - Connect the Campus (最小生成树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/476058

相关文章

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

C/C++随机数生成的五种方法

《C/C++随机数生成的五种方法》C++作为一种古老的编程语言,其随机数生成的方法已经经历了多次的变革,早期的C++版本使用的是rand()函数和RAND_MAX常量,这种方法虽然简单,但并不总是提供... 目录C/C++ 随机数生成方法1. 使用 rand() 和 srand()2. 使用 <random

Flask 验证码自动生成的实现示例

《Flask验证码自动生成的实现示例》本文主要介绍了Flask验证码自动生成的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 目录生成图片以及结果处理验证码蓝图html页面展示想必验证码大家都有所了解,但是可以自己定义图片验证码

Python如何在Word中生成多种不同类型的图表

《Python如何在Word中生成多种不同类型的图表》Word文档中插入图表不仅能直观呈现数据,还能提升文档的可读性和专业性,本文将介绍如何使用Python在Word文档中创建和自定义各种图表,需要的... 目录在Word中创建柱形图在Word中创建条形图在Word中创建折线图在Word中创建饼图在Word

nginx生成自签名SSL证书配置HTTPS的实现

《nginx生成自签名SSL证书配置HTTPS的实现》本文主要介绍在Nginx中生成自签名SSL证书并配置HTTPS,包括安装Nginx、创建证书、配置证书以及测试访问,具有一定的参考价值,感兴趣的可... 目录一、安装nginx二、创建证书三、配置证书并验证四、测试一、安装nginxnginx必须有"-