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

相关文章

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

python如何生成指定文件大小

《python如何生成指定文件大小》:本文主要介绍python如何生成指定文件大小的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python生成指定文件大小方法一(速度最快)方法二(中等速度)方法三(生成可读文本文件–较慢)方法四(使用内存映射高效生成

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr

PyQt5+Python-docx实现一键生成测试报告

《PyQt5+Python-docx实现一键生成测试报告》作为一名测试工程师,你是否经历过手动填写测试报告的痛苦,本文将用Python的PyQt5和python-docx库,打造一款测试报告一键生成工... 目录引言工具功能亮点工具设计思路1. 界面设计:PyQt5实现数据输入2. 文档生成:python-

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注